2017-06-15 172 views
0
sp = int(input("the dp is $")) 
    print(sp) 
    while True: #make sure they only put number 
     try: 
      int(sp) 
     except ValueError: 
      try: 
       float(sp) 
      except ValueError: 
       print("This is not a number. Please put a valid price.") 
     print("Please enter a valid number") 
    #This is to ensure that the customer is entering a valid variable, not too high or low 
    if sp <=100: print("Your price is too low. Please enter a higher and valid price.") 
    if sp >=10000: print("Your price is too high. Please enter a lower and valid price") 

當我嘗試放入過高或過低數字時,消息錯誤有效,但如果輸入字母,程序會報錯。ValueError:對於int()以10爲基數的無效文字:

+2

你會得到哪個錯誤,什麼是完整的回溯?你怎麼看到最後兩行:你的'while'循環無法結束。 –

+3

是的,因爲你正在使用'sp = int(input(「the dp is $」)',讓你的整個'while True'循環變得毫無意義,除去調用'int',這就是循環和try '是! –

回答

-1

縮進很重要。我認爲這是你想要做的?另外,由於你的輸入()是在循環之外,所以當有人輸入一個無效的數字時,它會一直循環着這個無效的數字。

while True: #make sure they only put number 
    sp = int(input("the dp is $")) 
    print(sp) 
    try: 
     int(sp) 
    except ValueError: 
     try: 
      float(sp) 
     except ValueError: 
      print("This is not a number. Please put a valid price.") 

    #This is to ensure that the customer is entering a valid variable, not too high or low 
    if sp <=100: print("Your price is too low. Please enter a higher and valid price.") 
    elif sp >=10000: print("Your price is too high. Please enter a lower and valid price") 
    else: print("Success") 
0
sp = int(input("the dp is $")) 

你已經定義變量爲一個整數位置。

沒有錯誤信息我只能推測,但我已經在這裏發生的圖像,因爲一個字符串不能轉換爲int。

相關問題