Python Forum

Full Version: Simple Python program not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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.
You need to convert user_guess to an int:
user_guess = int(input("Please enter your Magic Number guess. "))
Thanks!. Your help and answer inspired me to investigate what was happening. Correct me if I am wrong, but the original line

user_guess = (input("Please enter your Magic Number guess. ")
resulted in user_guess holding a string. And comparing a numeric value with a string value will always(?) return false, right? Now with comparing an int to an int, the if statement is allowed to return what I needed.

Again, thanks for your help.
Correct