-1

The scenerio is: I’m asking user to input only 5 numbers between 1 and 5 but how do we fix if he input 6 or more and 1 or less and also how to repeat the program again and again if he inputted negative integers like -1,-2. Also, if he input strings, I want to show the users to input only integers

Num = [0,0,0,0,0]

for i in range (5):
    Num[i] = int(input("Put the numbers only between 1 and 5!”))
print("Your numbers are ", Num)

I tried try except value error but I’m not sure how to put it and expected program to run without errors

Rathish Kumar B
  • 1,271
  • 10
  • 21
Thiha Hein
  • 31
  • 3
  • If you need it to restrict the input to be in the range 1-5, you actually can just add a variable and set the input into it i.e. `a = input()` then next line will be `if a in [1:6]:` then you can do the logic `num[i] = int(a)` – Dhana D. Oct 26 '22 at 06:23

1 Answers1

0

Try doing this:

Num = [0,0,0,0,0]

count = 0
while count < 5:
  num = int(input("Put the numbers only between 1 and 5!"))
  if num > 0 and num < 6:
    Num[count] = num
    count += 1
  else:
    print("You must enter values from 1 to 5!")
  
print("Your numbers are ", Num)
Sayad Ahmed Shaurov
  • 499
  • 1
  • 7
  • 10