2017-08-01 96 views
-1
#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.') 

這是第一次,你需要輸入任何你喜歡的名字,然後程序會從你進入那些決定一個名字,然後你必須輸入正確的姓名,他是片段現在想着。我輸入了所有的名字,但都沒有工作。片段無法正常工作

+0

想想對象你」重新使用:'secret'是一個單元素列表,它不會等於輸入字符串。 – jonrsharpe

回答

0

這很簡單。

你的祕密變量是一個列表。 這一行後:

secret = random.choice(secretnames) 

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

你需要改變你的,如果這樣的語句:

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

謝謝。你解決了我的問題。 –

0

secretnames是列表的元組,由於某種原因。 random.choice(secretnames)將返回其中一個子列表,它本身包含單個名稱。

你應該只使用一個單獨的列表,並追加到它:

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