This question already has an answer here:
I just started building a "high low" game in python. In the game there are 4 possible outcomes that have to be coded into IF statements (at least I think they have to be if statements). There are no syntax errors or anything like that. But for some reason no matter what your input is, the terminal will always say "The number was (insert number)! You lost!". I'm assuming it's because it's the first IF statement.
here is an example of what my terminal looks like when I try to run the program (I put "terminal: and user:" in front of the text so it would be easier to differentiate from what the computer is saying VS what the user inputed.):
terminal: Would you like a number? user: yes
terminal: Will the next number be higher or lower than 3?
user: higher
terminal: The number was 9! You lost 1!
I have turned all ELIF statements into IF statements and that has not worked. I also have tried using a IF, ELIF, ELSE but I couldn't figure out a way to only have 3 statements when I have 4 possible outcomes.
from random import randrange
rn = (randrange(1, 10))
rnTwo = (randrange(1, 10))
finRn = abs(rn - rnTwo)
hhr = "higher" or "Higher"
lwr = "lower" or "Lower"
hh = "high" or "High"
ll = "low" or "Low"
userAns = input("Would you like a number? ")
if userAns == "yes":
userHl = input("Will the next number be higher or lower than " +
str(rn) + "? ")
if rn > rnTwo and userHl == hhr or hh:
print("The number was " + str(rnTwo) + "! You lost! ")
elif rn < rnTwo and userHl == hhr or hh:
print("You are correct! the number was " + str(rnTwo) + "! ")
elif rn < rnTwo and userHl == lwr or ll:
print("The number was " + str(rnTwo) + "! You lost! ")
elif rn > rnTwo and userHl == lwr or ll:
print("You are correct! the number was " + str(rnTwo) + "! ")
The expected results were that the computer would respond accurately to the users input. What actually happens is that the first if statements print will be printed (print("The number was " + str(rnTwo) + "! You lost! ")) no mater what the user input is.
Aucun commentaire:
Enregistrer un commentaire