-2

I'm having trouble with my code and am wondering if there is a way to check to see if a value is both an integer/number and if the value is <=3. This is the code I need help with:

while SystemError:
    s=int(input('You look arround to find your barings when you spot a light in the distance symbolyzing that there is a town/village up ahead you have three choices \nchoice 1 you can fly there with your wings, \nchoice 2 you can move through the trees as a shortcut, \nand choice 3 you can walk on the road and hopefully you dont get lost. which is it gonna be \nchoice 1, \n2, \nor 3?:  '))
    if not s<=3:
        print('im sorry you have inputed an incorrect value')
        continue
    else:
        break
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 1
    What kinds of trouble are you having? – quamrana Nov 18 '20 at 14:12
  • the problems im having is when i run the code and test by pressing a letter instead of a number and the code crashes ive even tried removing the .int value but i cant get it to move on to the next set of code – Tristan Lauzon Nov 18 '20 at 14:16
  • 1
    @TristanLauzon Get rid of `int(...)` and then do `if s not in {'1', '2', '3'}':`. (PS: you should also use `while True:`, not `while SystemError:`). – ekhumoro Nov 18 '20 at 14:22

3 Answers3

2

Add this to your code and it should be running fine-

    try:
        s = int(input('You look arround to find your barings when you spot a light in the distance symbolyzing that there is a town/village up ahead you have three choices \nchoice 1 you can fly there with your wings, \nchoice 2 you can move through the trees as a shortcut, \nand choice 3 you can walk on the road and hopefully you dont get lost. which is it gonna be \nchoice 1, \n2, \nor 3?: '))
        break
    except ValueError or s > 3 or s <= 0:
        print("I'm sorry you have inputed a wrong value\n \n")
        continue
1
MAX_TRIAL = 5 
curr_input_count = 0
while curr_input_count <= MAX_TRIAL:
   try:
     str_input = input("Enter a value:")
     curr_input_count += 1
     int_value = int(str_input)
     if not int_value <= 3:
       print("Incorrect value entered. Please try again.")
       continue
     else:
       break
    except Exception:
      print('Invalid input entered')
      continue
if curr_input_count == MAX_TRIAL:
  print('Max Input limit exceeded. Bye!!!')
Riyas
  • 268
  • 2
  • 11
0

use try and except

try:
        s = int(input('''You look arround to find your barings when you spot a light in the distance symbolyzing that there is a town/village up ahead you have three choices \nchoice 1 you can fly there with your wings, \nchoice 2 you can move through the trees as a shortcut, \nand choice 3 you can walk on the road and hopefully you dont get lost. which is it gonna be \nchoice 1, \n2, \nor 3?:'''))
        
    except ValueError or s > 3 :
        print('''im sorry you have inputed an incorrect value''')
        
Adam
  • 2,820
  • 1
  • 13
  • 33
harsh
  • 11
  • 2