My game cant differentiate between no input and a numarical input

Viewed 38

Here is the code to the project, yes i know the game over screen is trash.

also, i am running this on trinket.io, so some stuff might not be supported.
the issue is even if i give t a number, like 2, it just gives me the "please enter a number between 1 and 3" screen.

i have no idea as to why its doing this, it might be somthing in the way of how it cheks for null inputs.

so if you have any ideas as to why it dose this, please let me know.

import random,os,time
correct = 0
continue_=1

def clear():
 os.system("cls")

def prompt(prompt):
 return(input(prompt))

 def prompt(prompt):
 return(input(prompt))
#above, defining and importing things

print "Welcome to: PRESS YOUR LUCK\n now with a 75% chance of winning"
raw_input("\nnext slide (press enter to continue)")
clear()

print "the goal... to get as many right in a row as possible\n you will pick 1, 2, or 3 if you are right you continue on\nif not you fail."
raw_input("\nnext slide (press enter to continue)")
clear()

#above, the 2 slides you see when you start the game
while continue_ == 1:
 number_selected = (prompt("1, 2 or 3 \n>>>"))
 random_number = random.randint(1,3)
 random_number_mk2 = random.randint(1,3)

 #above, prompt for asking for the input number, then the 2 random number gens
 if number_selected == (1,3):

 #above, part 1 of finding null inputs
  if number_selected == random_number or number_selected == random_number_mk2:
    correct +=1
    print "good job you have got, " + str(correct) + " correct!"
    time.sleep(2)
    clear()

  #above, if you got one of the numbers right, you get this message
  if number_selected != random_number and number_selected != random_number_mk2:
   print"  _____                         ____"                 
   print" / ____|                       / __ \ "
   print"| |  __  __ _ _ __ ___   ___  | |  | |_   _____ _ __"
   print"| | |_ |/ _` | '_ ` _ \ / _ \ | |  | \ \ / / _ \ '__|"
   print"| |__| | (_| | | | | | |  __/ | |__| |\ V /  __/ |"   
   print" \_____|\__,_|_| |_| |_|\___|  \____/  \_/ \___|_|"
   print "the numbers were",(random_number),"and",(random_number_mk2),"\nyou got:",(correct),"correct!"

   #if you got it wrong, you get this insted
   prompt("press one to continue, press zero to quit")
   if continue_==0:
    exit()
   else: 
    pass
    clear()

    #system to promp the user to continue, or quit
 else:
   clear()
   print("please put a number between 1 and 3")
   continue_ = 1
   time.sleep(2)
   clear()
   #this system is part 2 of checking for no input when prompted
1 Answers

I see at least 2 reasons why it doesn't work:

1) input returns a string, even if you just type a number. E.g. :

>>> answer = input("please type a number\n")
please enter a number
3
>>> type(answer)
<class 'str'>

So your prompt() function returns a string, not an int.

If you absolutely need this answer to be an int, a suggestion:

def prompt(prompt_message):
    return int(input(prompt_message))

This will raise a ValueError if you type something else than a number when prompted, since this can't be converted to an int (ex : type "abc", you'll get an error).

By the way, I'm wondering why you wrote prompt() in that way:

def prompt(prompt):
 return(input(prompt))

 def prompt(prompt):
 return(input(prompt))

But maybe it's just a double copy-paste by mistake ?

2) you wrote this:

number_selected = (prompt("1, 2 or 3 \n>>>"))
(...)
if number_selected == (1,3):

The last line means literally "if number_selected is the tuple (1,3)". What you meant is:

if number_selected in [1,2, 3] ("if number_selected is in the list composed of int values 1, 2 and 3)

Hope it helps!

Related