Issue
import random
guess = input("Head or Tail? ")
head_tails = ["Head", "Tail"]
def coin_flip():
random_number = [random.randint(0,1)]
for rando in random_number:
if rando == 0:
answer = head_tails[rando]
elif rando == 1:
answer = head_tails[rando]
user_guess = ""
#User_guess is always "False", even though the user(input) guessed the right outcome...
if guess == "Head" and random_number == 0:
user_guess = "True"
elif guess == "Tail" and random_number == 1:
user_guess = "True"
else:
user_guess = "False"
if user_guess == "True":
print("You are right!")
print("It was:", answer)
elif user_guess == "False":
print("You guessed wrong! You guessed:", guess)
print("It was:", answer)
coin_flip()
Why does user_guess
always equal "False"
?
Solution
Since random_number is list, it can not be equal to an integer - a list is not an integer. To make the if-statement work, put brackets around the integers either random_number = [0]
or random_number = [1]
. This will work since the number is within a list, i.e. in random_number
. You can also change random_number
so it is not a list and therefore remove for rando in random_number:
and change it to answer = heads_tails[random_number]
and keep random_number == 0
. This will work since random_number
is not a list, thus it can be equal to an integer.
Answered By - Ponta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.