1

I'm a beginner in python, when I run the script it doesn't say there's an error I need to fix but when it gets to the part of the if statement it doesn't report anything.

import random

username = input("What is your name?")
print("Hello",username)
highestValue = input("What do you want your highest value to be?")
highestValue = int(highestValue)
randomNumber = random.randint(0,highestValue)
print(randomNumber)
guessedNumber = input("Take a guess at my secret number")

if guessedNumber == randomNumber:
  print("Congratulations! You guessed my secret number")
3
  • 1
    Looks like guessedNumber is a string while randomNumber is a number. Commented Apr 6, 2022 at 0:30
  • input returns a string for guessedNumber, and you haven't converted that to an int. Even if you guessed the correct number, 16 does not equal '16', so your if statement would evaluate to False. You might want to add an else statement so you can tell that at least something is happening when you guess incorrectly. Commented Apr 6, 2022 at 0:33
  • Thank you I just used and the type() method and it's stored as a string Commented Apr 6, 2022 at 0:35

4 Answers 4

1

The problem here is that guessedNumber is a str, as input() returns a string.

To solve this, either

1) Cast randomNumber to a str using str(randomNumber)

or

2) Cast guessedNumber to an int using int(guessedNumber)

The first solution is safer, as ints can always be casted to a str, whereas strs can only be cast to an int when it contains a number. However, you will need to use the second solution if you want to compare the numbers by checking if one is higher or lower than the other. To prevent errors with the second solution, you can use if guessedNumber.isnumeric() before casting it to check that the user inputted a number.

Sign up to request clarification or add additional context in comments.

Comments

0

This is happening because when you use input in python, the value is a string by default, and when comparing a string to a number, the result will always be False.

To correct your code, use int(input()):

guessedNumber = int(input("Take a guess at my secret number"))

Comments

0

Any input in python is str, to make it work, you just need to change input:

import random

username = input("What is your name?")
print("Hello",username)
highestValue = input("What do you want your highest value to be?")
highestValue = int(highestValue)
randomNumber = random.randint(0,highestValue)
print(randomNumber)
guessedNumber = int(input("Take a guess at my secret number")) #Make it int value

if guessedNumber == randomNumber:
  print("Congratulations! You guessed my secret number")

Comments

0

This should work:

import random
username = input("What is your name?")
print("Hello",username)
highestValue = input("What do you want your highest value to be?")
highestValue = int(highestValue)
randomNumber = random.randint(0,highestValue)
print(randomNumber)
while True:
    guessedNumber = int(input("Take a guess at my secret number"))
    if guessedNumber == randomNumber:
        print("Congratulations! You guessed my secret number")
        break

Output:

What is your name? bob
Hello bob
What do you want your highest value to be? 10
2
Take a guess at my secret number 5
Take a guess at my secret number 3
Take a guess at my secret number 4
Take a guess at my secret number 2
Congratulations! You guessed my secret number

I added a while loop so the program keeps running until the user gets the number right and I had to convert guessedNumber to an int as randomNumber is also an int

2 Comments

I might be mistaken here, but isn't the code you posted the exact same as what the original poster posted?
oops, I did not copy the right code :) I just edited it include the new code. I guess i just forgot to press ^C

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.