2013-09-29 17 views
1

我必须创建一个游戏,电脑会随机选择一个单词,玩家必须猜测这个单词。计算机告诉玩家这个词有多少个字母。然后玩家有五次机会询问单词中是否有字母。电脑只能用"yes""no"进行响应。然后,玩家必须猜测这个词。 我只有:如何从一个字符串中提取信息并输出它?

import random 
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle") 
word = random.choice(WORDS) 
print(len(word)) 
correct = word 
guess = input("\nYour guess: ") 
if guess != correct and guess != "" : 
     print("No.") 

if guess == correct: 
    print("Yes!\n") 

我不知道该怎么办这个问题。

+1

你会如何做手工吗?写出一些伪代码,即使它真的是高级别的。 – Blender

+0

使用计数器和一个while循环 –

回答

0

您正在寻找的东西像下面

import random 

WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle") 

word = random.choice(WORDS) 
correct_answer = word 
max_guesses = 5 

print("Word length:", len(word)) 
print("Attempts Available:", max_guesses) 


for guesses in range(max_guesses): 
    guess = input("\nEnter your guess, or a letter: ") 
    if guess == correct_answer: 
     print("Yay! '%s' is the correct answer.\n" % guess) 
     break 
    elif guess != "": 
     if guess[0] in correct_answer: 
      print("Yes, '%s' appears in the answer" % guess[0]) 
     else: 
      print("No, '%s' does not appear in the answer" % guess[0]) 
else: 
    print("\nYou ran out of maximumum tries!\n") 
+0

谢谢你的作品 – user2829036

1

我假设你想让如果一个字母在单词,5倍,用户要求计算机。如果是这样,这里是代码:

for i in range(5): #Let the player ask 5 times 
    letter = input("What letter do you want to ask about? ")[0] 
    #take only the 1st letter if they try to cheat 

    if letter in correct: 
     print("yes, letter is in word\n") 
    else: 
     print("no, letter is not in word") 

的关键是for循环中in操作。

相关问题