Python Forum
What was my mistake in this Python code (easy)?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What was my mistake in this Python code (easy)?
#1
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
Reply
#2
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 !
Reply
#3
I think you want if guess == secretNumber:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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. ​
Reply
#5
(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.")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code problem - probably easy fix? colin_dent 5 825 Jun-30-2023, 01:55 PM
Last Post: deanhystad
  [Solved] Please, help me find a simple mistake AlekseyPython 2 1,701 Jun-17-2021, 12:20 PM
Last Post: AlekseyPython
  [split] Could you please clarify where i did mistake also how run without admin right Abubakkar 1 1,753 Jun-14-2021, 09:32 AM
Last Post: Larz60+
  creating simplex tableau pivot program easy or difficult for a beginner in Python? alex_0 2 2,535 Mar-31-2021, 03:39 AM
Last Post: Larz60+
  Problem with very easy code. janekk9002 1 1,760 Dec-10-2020, 12:57 PM
Last Post: buran
  Please help to me to find my mistake in code leonardin 2 1,801 Nov-29-2020, 04:17 PM
Last Post: Larz60+
  minor mistake in code for factorial spalisetty06 2 1,854 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Simple mistake about for Nomatter 4 2,183 Jul-16-2020, 02:24 PM
Last Post: Nomatter
  Install Mistake jlerette5 1 1,857 Feb-18-2020, 12:19 AM
Last Post: jefsummers
  countdown script not working..plz help what is mistake randyjack 1 2,079 Oct-28-2019, 06:57 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020