2015-09-26 20 views
0
print("=======================PHONE ONE INPUT=============================") 
    global name1 
    name1=input("1).  What brand is your phone? ") 
    condition=input("2).  What is the condition of your phone (new or used)? ") 
    camera=input("3).  Does the phone have a Camera (true or false)? ") 
    GPS=input("4).  Does the phone have GPS (true or false)? ") 
    WiFi=input("5).  Does the phone have WiFi (true or false)? ") 
    global price1 
    price1=input("6).  Enter phone price($0-$1000) : ") 
    if (price1>"1000" or price1<"0"): 
     print("Invalid input, please re-run the simulation") 
     def main(): 
    else: 
     computeValue(condition,GPS,WiFi,camera) 
     PhoneValue1=PhoneValue 

我試圖讓上面的代碼重新運行,如果用戶輸入一個大於1000或小於0的值。我該怎麼做呢?重新運行基於輸入的代碼

+0

請格式化您的代碼,以便我們可以閱讀它。選擇代碼,然後點擊輸入框頂部的大括號。謝謝。 – saulspatz

+0

'而user_input> 1000或user_input <0:...'? – alfasin

+0

如果我將它更改爲while循環,它將不會繼續,如果值介於0-1000之間 – noahdukehart

回答

0

你需要投price1整數 - 使用int() - 而不是使用它作爲一個字符串,如果你想使用<>的數值比較:

price1=int(input("6).  Enter phone price($0-$1000) : ")) 
if (price1> 1000 or price1 < 0): 
    print("Invalid input, please re-run the simulation") 
    main() 

而且,如上圖所示,你需要使用main()來運行你的主要功能。 def main():定義了稱爲main的功能。

但是,您可能只是想重複此輸入,而不是運行整個函數。您可以使用while循環來執行此操作,如下所示。

valid_price = False 
while (not valid_price): 
    price1 = int(input("6).  Enter phone price($0-$1000) : ")) 
    if (price1> 1000 or price1 < 0): 
     valid_price = True 
    else: 
     print("Invalid input.") 

的Python 3 \ Python 2的視差

在Python 2,input將評估輸入表達式並返回的評價值,所以輸入要麼52 + 3會給你整數5,例如。

在Python 3,input相同raw_input在Python 2,其解釋給定的輸入作爲一個字符串,因此5會給你"5"2 + 3會給你"5"。這就是爲什麼我們需要使用上面的int()將輸入轉換爲整數。