0

Random Number Generator - Guess It

Did a much more complex variant of this. (250+ lines) But when I tested it, even if I guessed the number correctly(it was between 1-20) it didn't accept it. Every stage, I programmed the computer to tell me a statement about the number, using if, elif and else commands. When I guessed the number, it just said the number is incorrect and told me a new statement about number. Could someone help, please? In this mini variant as well, if I get the correct answer, it still responds to the if statement.

import random 

list = [1,2,3]
target = print(random.choice(list))

guess = input("What is the target number?")
if guess == target :
   print("Well done.")
else:
    print("The number you have guessed isn't correct.")
kauthy
  • 11
  • 1
  • 2
    `input()` returns a string, not a number, so `1 == "1"` will always be `False` – ddejohn Feb 02 '22 at 17:07
  • 2
    Also, don't use `list` as a variable name. It's a built-in function and you're overwriting it. Choose a different, more descriptive name. – ddejohn Feb 02 '22 at 17:08
  • Also, as @oflint_ mentions, remove the `print()` from the `target` assignment. The `print()` function returns `None` so you're actually always doing `None == "1"` (or `"2"`, etc). – ddejohn Feb 02 '22 at 17:11
  • yes I wrote the longer function without the print function. – kauthy Feb 02 '22 at 17:51

1 Answers1

0

You need to convert the guess into an integer before comparison. and remove the print statement from the variable definition. You also need to rename the list variable as it is a protected name.

import random 

listNumbers = [1,2,3]
target = random.choice(listNumbers)

guess = int(input("What is the target number?"))
if guess == target :
   print("Well done.")
else:
    print("The number you have guessed isn't correct.")
oflint_
  • 297
  • 1
  • 8