Jian Calazan Jian Calazan - 4 months ago 22
Python Question

Color sequence game in Python

The code below is for color sequence guessing in python.

import random
import time

def colorgame():
r='red'
b='blue'
g='green'
y='yellow'
v='violet'
bl='black'
o='orange'
w='white'
m='magenta'
colors = [r, b, g, y, v, bl, o, w, m]
sequence=[]
win = True

while win == True:
for i in colors:
sequence.append(random.choice(colors))
print(sequence)
time.sleep(3)
answer=input("Enter answer here: ")
if answer == sequence:
print("Correct")

else:
win=False


In the first iteration say
sequence = 'red'
user enters
red
and it prints
"Correct"
. But, when it comes to the second iteration (say)
sequence = ['red', 'green']
and user enters
'red' 'green'
It is not returning correct. Please help me in correcting the code so that the user input is accepted as list and compared to the existing list (
sequence
)

Answer

After going through the comments and clear understanding of your problem I got something to answer:

answer = input("Enter answer here:").split()

Edited: Thanks for @mhawke

Comments