2017-06-15 97 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。

相关问题