2017-06-15 17 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:基数10のint()のリテラルが無効です。

+2

どのエラーが表示されますか、完全なトレースバックは何ですか?最後の2行にどのように到達すればよいでしょう:あなたの 'while'ループは終了する方法がありません。 –

+3

はい、あなたは 'sp = int(input(" dpは$ ")'を使用しているので、 'while True'ループ全体を無意味にしています。 'のためです! –

答えて

-1

インデントが重要です。私はこれがあなたがしようとしていたものだと思いますか?また、あなたのinput()がループの外にあったので、誰かが無効な数値を入力した場合、その無効な数値を永久にループし続けます。

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に変換できないため、ここではイメージが既に発生しています。

関連する問題