简体   繁体   English

Python-猜文字游戏

[英]Python - Guess a word game

Here is my code: 这是我的代码:

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!")

Problem is when i run my code, and let's say i type letter "s", my sublime freeze and i get message "Sublime 3 is not responding.." and i have to turn it off. 问题是当我运行我的代码时,假设我键入字母“ s”,我的崇高冻结,我收到消息“ Sublime 3没有响应..”,我必须将其关闭。 Maybe it's while loop? 也许是while循环? Infinite? 无穷?

You have an infinite loop because your 'guesses' variable is not being inceremented in the while loop; 您有一个无限循环,因为while循环中没有增加'guesses'变量; move the 'guesses += 1' into the loop itself eg 将'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

There are a few things. 有几件事。

guesses =+ 1 should be guesses += 1 and at the top of the while loop before any if statements I think your guess = raw_input should be within the while loop before any of the if statements. 猜测= + 1应该是猜测+ = 1,并且在if语句之前的while循环的顶部,我认为您的guess = raw_input应该在if语句之前的while循环内。 So it will keep asking and counting for each guess until it reaches 5. The last if statement within the loop should be if guesses == 5 or guesses >= 5 instead of guesses > 5. 因此,它将一直对每个猜测进行询问并计数,直到达到5。循环中的最后一个if语句应该是ifgues == 5或guess> = 5而不是guess> 5。

Mostly just reordering things all within the while loop. 大多数情况下,只是在while循环中对所有事物进行重新排序。 Everything under the code within the while loop will be ran for every guess. while循环中代码下的所有内容都会针对每个猜测运行。

I changed it according to the above and it works great for me: 我根据上述内容进行了更改,对我来说非常有用:

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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM