sluggthesnail
sluggthesnail

Reputation: 11

if statement not reporting anything in python

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")

Upvotes: 1

Views: 58

Answers (4)

catasaurus
catasaurus

Reputation: 966

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

Upvotes: 0

Vazno
Vazno

Reputation: 71

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")

Upvotes: 0

KingsDev
KingsDev

Reputation: 660

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.

Upvotes: 1

Gabriel Caldas
Gabriel Caldas

Reputation: 381

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"))

Upvotes: 0

Related Questions