代码段无法正常工作

时间:2017-08-01 08:47:41

标签: python

#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.')

这是首先你需要输入你喜欢的名字的片段然后程序将从你输入的名字中确定一个名字,然后你必须输入他正在考虑的正确名字。我输入了所有的名字,但没有一个在起作用。

2 个答案:

答案 0 :(得分:0)

出于某种原因,

secretnames是一个列表元组。 random.choice(secretnames)将返回一个子列表,其中包含一个名称。

您应该只使用一个列表并附加到它:

secretnames = []
...
secretnames.append(input())

答案 1 :(得分:0)

那很简单。

您的秘密变量是一个列表。 在这一行之后:

    secret = random.choice(secretnames)

秘密指向列表:[' RandomName']

您需要更改if语句:

    if ans == secret [0]:
      print('Good job. You give the correct answer in ' + str(i) + ' guesses.')
    elif ans != secret[0]:
      print('Wrong Answer.')