Simple Python program not working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Simple Python program not working (/thread-7753.html) |
Simple Python program not working - AudioKev - Jan-23-2018 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. RE: Simple Python program not working - Larz60+ - Jan-23-2018 You need to convert user_guess to an int: user_guess = int(input("Please enter your Magic Number guess. ")) RE: Simple Python program not working - AudioKev - Jan-23-2018 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. RE: Simple Python program not working - Larz60+ - Jan-23-2018 Correct |