0

I am trying to make my program print(invalid input, try again) if the user types a string instead of an integer.

num_dice = 0
while True:
   if num_dice < 3 or num_dice > 5 or num_dice:
       num_dice = int(input("Enter the number of dice you would like to play with 3-5 : "))
       print("")
       print("Please enter a value between 3 and 5.")
       print("")
       continue
   else:
       break 
Joseph
  • 45
  • 2

1 Answers1

1

You can simply use the isnumeric keyword to check if it is an absolute number or not.

Example:

string1="123"
string2="hd124*"
string3="helloworld"

if string1.isnumeric() is True:
    #Proceed with your case.
    inputs=string1

Documentation reference : https://www.w3schools.com/python/ref_string_isnumeric.asp

P.S. this will require you changing your input to string format, as isnumeric validates only string.

This below part I mean.

num_dice = str(input("Enter the number of dice you would like to play with 3-5 : "))

High-Octane
  • 1,104
  • 5
  • 19