Jan-23-2018, 07:12 PM
Greetings,
Learning Python for the first time. Initial assignment: write a program that has a variable with a known numerical value, ask a user to guess that number, and tell the user if their guess matched the known value.
Here is what I coded:
>>> magic_number_guess()
Please enter your Magic Number guess. 8
7
8
Your guess was not correct. Please try again.
>>> magic_number_guess()
Please enter your Magic Number guess. 7
7
7
Your guess was not correct. Please try again.
No matter the value of user_guess, it seems that the if statement is never true: Your guess is correct! is never the result even when inputting 7.
I can go back and create simple if/elif/else or if/else statements that work as expected. But in the case of the code above, not so much.
What am I doing incorrectly/not accounting for?
Thanks.
Learning Python for the first time. Initial assignment: write a program that has a variable with a known numerical value, ask a user to guess that number, and tell the user if their guess matched the known value.
Here is what I coded:
def magic_number_guess(): magic_number = 7 user_guess = input("Please enter your Magic Number guess. ") print(magic_number) print(user_guess) if user_guess == magic_number: print("Your guess is correct!") else: print("Your guess was not correct. Please try again.")Typical results:
>>> magic_number_guess()
Please enter your Magic Number guess. 8
7
8
Your guess was not correct. Please try again.
>>> magic_number_guess()
Please enter your Magic Number guess. 7
7
7
Your guess was not correct. Please try again.
No matter the value of user_guess, it seems that the if statement is never true: Your guess is correct! is never the result even when inputting 7.
I can go back and create simple if/elif/else or if/else statements that work as expected. But in the case of the code above, not so much.
What am I doing incorrectly/not accounting for?
Thanks.