创建一个猜谜游戏。循环问题。我是一名新程序员,也是第一次发帖。任何信息都有帮助 - 谢谢

发布于 2025-01-20 03:11:57 字数 554 浏览 1 评论 0原文

我对 Python 世界是全新的,所以请原谅我,这感觉像是一个新手类型的问题。我正在创建一个小型猜谜游戏,用户将猜测一个单词。我遇到的问题是,一旦用户猜出正确的单词,它就会给他们正确的输出,然后它将循环回到开始的问题“如果你想退出,请插入 q 退出或 y 播放。”

这就是我想要的,但目前它只是输出上一场比赛的答案。有什么想法如何循环但让系统选择一个新的随机单词?这是我的 WHILE 循环:

while loop:
print ("If you want to quit, please insert q for QUIT or y for PLAY")
play = input()
if play == 'q':
    loop = False
elif play == 'y':

    while guess != word:
        guess = input("Guess a word: ")

    if guess == word: 
        time.sleep(.9)
        print (f"Congrats, you won! the word was {word}")

I am brand new to Python world, so forgive me, this feels like a newbie type question. I am creating a small guessing game where the user will guess a word. The problem I am having is that once the user guesses the correct word, it gives them the correct output and then it will loop back to the beginning question "If you want to quit, please insert q for QUIT or y for PLAY."

This is what I want, but currently its simply outputting the answer from the previous game played. Any ideas how to loop back but have the system select a NEW random word? Here is my WHILE loop:

while loop:
print ("If you want to quit, please insert q for QUIT or y for PLAY")
play = input()
if play == 'q':
    loop = False
elif play == 'y':

    while guess != word:
        guess = input("Guess a word: ")

    if guess == word: 
        time.sleep(.9)
        print (f"Congrats, you won! the word was {word}")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2025-01-27 03:11:57

假设您已经编写了一个逻辑来选择随机单词,只需在 while 循环内调用该函数即可。

while loop:
    word = select_random_word()

另外,在用户猜出每个单词后,将其值清空

guess == ''

Supposing you already coded a logic to select the random word, just call the function inside the while loop.

while loop:
    word = select_random_word()

Also, after the user guesses each word, empty its value

guess == ''
独守阴晴ぅ圆缺 2025-01-27 03:11:57

如果将变量存储在数组中,则应在循环末端将数组递增一个数组,或者使用类似的时间将随机数与种子一起使用。每次您运行游戏时,这都会使单词订单不同。

import numpy as np
import time 
np.random.seed(int(time.time()))
np.random.randint(low = 1, high = 10, size = 10)

然后,只需将大小更改为您数组中的数字量即可。但是,此方法不会消除多次显示的相同单词。

如果您要在代码开头选择单词以及存储单词的位置,我可以提供更多帮助。

If you are storing the variables in an array, you should either increment the array by one at the end of the loop or use a random number with a seed using time like so. This would make the word order different every time you run the game.

import numpy as np
import time 
np.random.seed(int(time.time()))
np.random.randint(low = 1, high = 10, size = 10)

Then just change size to the amount of numbers you have in your array. This method doesn't eliminate the same word showing up multiple times though.

I could help more if you were to give how you select the word at the beginning of the code, and where your words are stored.

变身佩奇 2025-01-27 03:11:57

您只需要初始化单词并猜测游戏何时开始。

elif play == 'q':
     word = (Select your word from where you have your words)
     guess = None
     ...

you just need to initialize the word and guess when your game begins.

elif play == 'q':
     word = (Select your word from where you have your words)
     guess = None
     ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文