So as part of this Python I'm currently taking one of the exercises is to create a guessing game which run in the terminal. Obviously there are multiple ways of doing this and instead of simply watching the solutions video I tried to do it myself first. My code runs with no errors and as far as I can tell it should do the job, however it doesn't. Instead of just looking up a solution on here and rewriting the whole thing the just copies what someone else did I was wondering if someone could help me out with understanding why my code doesn't do what I expected it to?
It's listed below, thank you.
import random
digits = list(range(10))
random.shuffle(digits)
one = digits[0:1]
two = digits[1:2]
three = digits[2:3]
digits = one, two, three
guess = input("What is your guess? ")
g_one = guess[0:1]
g_two = guess[1:2]
g_three = guess[2:3]
while(guess != digits):
print(digits)
if(guess == digits):
print("Congrats, you guessed correctly")
break
elif(g_one == one or g_two == two or g_three == three):
print("One or more of the numbers is correct")
guess = input("Try again: ")
elif(g_one != one and g_two != two and g_three != three):
print("None of those numbers are correct.")
guess = input("Try again: ")
else:
break
It seems you are not updating the values of g_one, g_two and g_three on every iteration.
digits = ([2], [3], [4]) # Assume this is the shuffling result, You are getting a tuple of lists here
guess = input("What is your guess? ") # 123
g_one = guess[0:1] #1
g_two = guess[1:2] #2
g_three = guess[2:3] #3
while(guess != digits): #123 != ([2], [3], [4]) string vs a tuple comparison here
print(digits)
if(guess == digits):
print("Congrats, you guessed correctly")
break
elif(g_one == one or g_two == two or g_three == three):
print("One or more of the numbers is correct")
guess = input("Try again: ") #You are getting the input but the splits are not updated
elif(g_one != one and g_two != two and g_three != three):
print("None of those numbers are correct.")
guess = input("Try again: ") #Same here. Not updating the splits
else:
break
I think that should explain it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.