Python:while循环外的for循环无法正常工作

时间:2019-02-06 05:43:52

标签: python

我想获得0到10之间的随机值大于8之前进行了多少次尝试。我有下面的代码可以正常工作:

@IBAction func updatedetails(_ sender: Any) {

        let imageobj = img.image!
        let data = imageobj.pngData()


        let acce:String = UserDefaults.standard.string(forKey: "access")!
        print(acce)

        let headers:HTTPHeaders = ["Authorization":"Bearer \(acce)"]


       // let userData:Data = try! JSONSerialization.data(withJSONObject: postParameters)
        let ImageData = imageobj.jpegData(compressionQuality: 0.5)

        Alamofire.upload(multipartFormData: { (multiFormData) in

            MultipartFormData.append(ImageData!, withName: photoURL, fileName: filename, mimeType: "image/jpeg")
            for (key, value) in Parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: "name")
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: "password")
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: "password_confirmation")
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: "city")
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: "state")
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: "address")
            }
        }, to: Constants.Userdetailsapi,method:.post,
           headers:headers,
           encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                    print(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        })    
}

但是,现在我想获得x第四次大于8之前的尝试次数。为此,我将for循环放在while循环之前,该循环如下所示:

import numpy as np

x = np.random.uniform(low=0, high=10, size=1)
attempt = 1

while(x < 8):
    attempt = attempt + 1
    x = np.random.uniform(low=0, high=10, size=1)

但是,这不符合我的预期。有人可以帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:2)

您想要获取在4个连续的跟踪中获得随机数8所需的尝试总数。试试这个:

>>> import numpy as np
>>> def fun():
...     x = np.random.uniform(0,10,1)
...     attempt = 1
...     while x<8:
...             attempt += 1
...             x = np.random.uniform(0,10,1)
...     return attempt 
>>> for i in range(0,4):
...     print("Trial" , i , "took" , fun(), "Attempts")

输出:

Trial 0 took 1 Attempts
Trial 1 took 1 Attempts
Trial 2 took 8 Attempts
Trial 3 took 3 Attempts

答案 1 :(得分:0)

问题是您没有重置x值。因此,一旦将变量设置为大于8的值,您的代码就不会再次进入while循环。您需要在while循环之前设置x = 0。

答案 2 :(得分:0)

您应该将代码添加到

for i in range(0,4):
    x = np.random.uniform(low=0, high=10, size=1)
    while(x < 8):
        attempt = attempt + 1
        x = np.random.uniform(low=0, high=10, size=1)

这将在x进入while循环之前重置x。没有此语句,您的控件仅进入一次while循环。

答案 3 :(得分:0)

有很多方法可以做到这一点。下面应该可以工作...

import numpy as np

successes = 0
rolls = 0
while (successes < 4):
    x = np.random.uniform(low=0, high=10, size=1)
    rolls += 1
    if x > 8:
        successes += 1

print(rolls)

答案 4 :(得分:0)

这不是for loop的情况。 试试这个:

while x < 8 and i <= 4:
    x = np.random.uniform(low=0, high=10, size=1)
    if x>8:
        i+=1
        x=np.random.uniform(low=0, high=10, size=1)
        attempt = 1
    print(attempt, x)
    attempt = attempt + 1