Python 代码段工作不正常
这是一段代码,首先你需要输入你喜欢的名字,然后程序将根据你输入的名字决定一个名字,然后你必须输入他现在想的正确名字。我输入了所有的名字,但没有一个有效 出于某种原因,secretnames是列表的元组Python 代码段工作不正常,python,Python,这是一段代码,首先你需要输入你喜欢的名字,然后程序将根据你输入的名字决定一个名字,然后你必须输入他现在想的正确名字。我输入了所有的名字,但没有一个有效 出于某种原因,secretnames是列表的元组random.choice(secretnames)将返回一个子列表,该子列表本身包含一个名称 您应该只使用一个列表并附加到其中: #This program will play a little game import random secretnames = ([], [], [], [],
random.choice(secretnames)
将返回一个子列表,该子列表本身包含一个名称
您应该只使用一个列表并附加到其中:
#This program will play a little game
import random
secretnames = ([], [], [], [], [], [])
print('Hi. Please enter your name in letters')
name = str(input())
print('Hi ' + name + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.')
print('But do not worry. I am going to let you to enter 6 names and i will choose one of them.')
print('After that you have to answer the correct name that i am thinking right now')
print('Please enter the first name in letters')
secretnames[0].append(input())
print('Please enter the second name in letters')
secretnames[1].append(input())
print('Please enter the third name in letters')
secretnames[2].append(input())
print('Please enter the fourth name in letters')
secretnames[3].append(input())
print('Please enter the fifth name in letters')
secretnames[4].append(input())
print('Please enter the sixth name in letters')
secretnames[5].append(input())
print('Alright ' + name + ' . Thank you for entering the names.')
secret = random.choice(secretnames)
for i in range(10):
print('Guess a name.')
ans = str(input())
if ans == secret:
print('Good job. You give the correct answer in ' + str(i) + ' guesses.')
elif ans != secret:
print('Wrong Answer.')
这很简单
您的秘密变量是一个列表。
在这一行之后:
secretnames = []
...
secretnames.append(input())
secret指向列表:['RandomName']
您需要像这样更改if语句:
secret = random.choice(secretnames)
想想你正在处理的对象:
secret
是一个元素列表,它不等于输入字符串。
if ans == secret [0]:
print('Good job. You give the correct answer in ' + str(i) + ' guesses.')
elif ans != secret[0]:
print('Wrong Answer.')