2017-06-28 86 views
0

這裏是我的代碼:的Python - 猜一個字遊戲

import random 

guesses = 0 #Number of tries to guess. 
print ("Welcome to Guess Me!") 
print ("\nYou have five guesses, choose letter and guess a word.") 
print("\nAll of luck!") 

def rand_word(): 
    f = open('sowpods.txt', 'r').readlines() #Sowpods is library with some random words 
    return random.choice(f).strip()          

word = rand_word() 
print word     #I printed a word just so i can test if is working. 

correct = word 
lenght = len(word)  #I want to tell them how many letters a word contain 
new_length = str(lenght) 

guess = raw_input("The word is " + new_length + " letters long. Guess a letter: ") 

while guesses < 5: 
    if guess not in word: 
     print("Sorry, this letter is not in word.") 
    else: 
     print("Correct, word contain this letter!") 

guesses =+ 1 

if guesses > 5: 
    final_answer = raw_input("You ran out of guesses, what is your answer?") 
    if final_answer == correct: 
     print("That's correct, congratulations you won!") 
    else: 
     print("Sorry, correct word is" + " " + word + " ,you can try again!") 

問題是,當我運行我的代碼,比方說,我輸入字母「S」,我的崇高凍結和我得到的消息「崇高3沒有反應..「,我不得不關掉它。也許這是while循環?無窮?

+1

是的,它看起來像有一個無限循環的'而猜測<5:' – davedwards

+2

提示:縮進是Python的關鍵! – eddiewould

+0

在'guesses = + 1'行之前的空行上沒有縮進,所以它不包含在while循環中,將相同的縮進添加到該空行,並改爲'guesses + = 1',應該可以工作。 – davedwards

回答

0

有幾件事。

guesses = + 1應該是猜測+ = 1,並且在任何if語句之前的while循環的頂部 我認爲您的guess = raw_input應該在任何if語句之前的while循環內。所以它會一直詢問和計算每個猜測,直到它達到5。循環內的最後一個if語句應該是如果猜測== 5或者猜測> = 5而不是猜測> 5。

大多隻是重新排列所有內容while循環。每個猜測都會在while循環內的代碼下運行。

我按照上面的改變它,它爲我的偉大工程:

while guesses < 5: 
    guesses += 1 
    guess = raw_input("The word is " + new_length + " letters long. Guess a letter: ") 
    if guess not in word: 
     print("Sorry, this letter is not in word.") 
    else: 
     print("Correct, word contain this letter!") 
    if guesses == 5: 
     final_answer = raw_input("You ran out of guesses, what is your answer?") 
     if final_answer == correct: 
      print("That's correct, congratulations you won!") 
     else: 
      print("Sorry, correct word is" + " " + word + " ,you can try again!") 

    Welcome to Guess Me! 

You have five guesses, choose letter and guess a word. 

All of luck! 
word 
The word is 4 letters long. Guess a letter: 1 
Sorry, this letter is not in word. 
The word is 4 letters long. Guess a letter: 2 
Sorry, this letter is not in word. 
The word is 4 letters long. Guess a letter: 3 
Sorry, this letter is not in word. 
The word is 4 letters long. Guess a letter: 4 
Sorry, this letter is not in word. 
The word is 4 letters long. Guess a letter: 5 
Sorry, this letter is not in word. 
You ran out of guesses, what is your answer?numbers 
Sorry, correct word is word ,you can try again! 
0

你有一個無限循環,因爲你的'猜測'變量沒有在while循環中被隱藏;將'guesses + = 1'移入循環本身,例如

while guesses < 5: 

    if guess not in word: 
     print("Sorry, this letter is not in word.") 
    else: 
     print("Correct, word contain this letter!") 

    guesses += 1