Python Forum
What was my mistake in this Python code (easy)? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: What was my mistake in this Python code (easy)? (/thread-22609.html)



What was my mistake in this Python code (easy)? - voltman - Nov-19-2019

Hi everyone,

Just learning python for the first time but I am having some difficulties.

I'm having some trouble with the code below:
minGuess = 1 
maxGuess = 6

#Ask the user for their name and their maxGuess
name = input("What is your name? ")
print("Hi " + name + "!")
print("Enter a number between: " + str(minGuess) + " and " + str(maxGuess))
guess = int(input("What is your guess? ")) 

#Generate a random number and tell the user if they won or lost   
import random
secretNumber = random.randint(minGuess, maxGuess)
if (guess == "secretNumber"):
  print("Congratulations, you got it right!")
else:
  print("You lose - the number was " + str(secretNumber))
print("Thank you for playing Guess the Number.")
It won't output "Congratulations, you got it right" even if I correctly guess.
Also, I noticed that if I change the bottom bit to the code below (then I always get the output that I lost the game even when I won):

if (guess != "secretNumber"):
  print("You lose - the number was " + str(secretNumber))
else:
  print("Congratulations, you got it right!")
print("Thank you for playing Guess the Number.")
What did I do wrong? Any help would be much appreciated. Blush

Thanks,
voltman


RE: What was my mistake in this Python code (easy)? - ThomasL - Nov-19-2019

if (guess == "secretNumber"):
First, no need to put brackets around comparison, but that´s not the problem.
What are you comparing there?
Definitely not that what you want!
You are comparing an integer value with a string !


RE: What was my mistake in this Python code (easy)? - ichabod801 - Nov-19-2019

I think you want if guess == secretNumber:.


RE: What was my mistake in this Python code (easy)? - jefsummers - Nov-19-2019

Let's clean this up. guess and secretNumber need to be the same type, integer or string but not both. Above works, for variety I will use string.
Note the use of f- strings to clean up the string concatenations
minGuess = 1 
maxGuess = 6
 
#Ask the user for their name and their maxGuess
name = input("What is your name? ")
print(f"Hi {name}!")
print(f"Enter a number between: {minGuess} and {maxGuess}")
guess = input("What is your guess? ") 
 
#Generate a random number and tell the user if they won or lost   
import random
secretNumber = str(random.randint(minGuess, maxGuess))
if (guess == secretNumber):
  print("Congratulations, you got it right!")
else:
  print(f"You lose - the number was {secretNumber}")
print("Thank you for playing Guess the Number.")
Output:
What is your name? Jeff Hi Jeff! Enter a number between: 1 and 6 What is your guess? 4 You lose - the number was 3 Thank you for playing Guess the Number. ​



RE: What was my mistake in this Python code (easy)? - snippsat - Nov-19-2019

(Nov-19-2019, 09:44 PM)jefsummers Wrote: Let's clean this up
Good,when first has started the clean up process can take it a step further Wink
4 space indentation,snake_case,remove a str and (),import first.
import random

min_guess = 1
max_guess = 6
# Ask the user for their name and their max_guess
name = input("What is your name? ")
print(f"Hi {name}!")
print(f"Enter a number between: {min_guess} and {max_guess}")
guess = int(input("What is your guess? "))

# Generate a random number and tell the user if they won or lost
secret_number = random.randint(min_guess, max_guess)
if guess == secret_number:
    print("Congratulations, you got it right!")
else:
    print(f"You lose - the number was {secret_number}")
print("Thank you for playing Guess the Number.")