python - Trying to make the script end if Var isn't an Int -


just making simple dice game python schoolwork. here's script i'm using:

from random import randint  print("welcome dice game.") print("") input("press enter begin.")  print("")  _choice = int(input("enter number of sides want on dice: ")) print("")  if isinstance( _choice, int ):     number = (randint(1,_choice))     print("your dice has been rolled...")     print("your number is:")     print(number)     print("")     input("press enter exit")     exit()    else:     print("you have selected invalid dice option")     print("dice game ending")     print("")     input("press enter exit manually")     exit() 

just remove whole else statement or 2 last lines it.

your script end reaching end of file

moreover, in example there's no way variable different int. in case user inputs incorrect data in:

_choice = int(input("enter number of sides want on dice: ")) 

int(...) throw exception terminate script when not catched.

you should rather in following way:

try:     _choice = int(input("enter number of sides want on dice: ")) except valueerror:     print "error log message here"     sys.exit()  # rest of script without unimportant type check: number = (randint(1,_choice)) print("your dice has been rolled...") print("your number is:") print(number) print("") input("press enter exit") sys.exit()   

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -