gpt4 book ai didi

Terminal asking for input twice [closed](两次要求输入的终端[关闭])

转载 作者:bug小助手 更新时间:2023-10-28 10:31:31 26 4
gpt4 key购买 nike




I am trying to make a word guessing program in Python since I am new to programming. I made it so that the terminal asks you for your guess, but when you respond, it asks you a second time. Why is this happening, and how can I fix it?

因为我是编程新手,所以我正在试着用Python语言编写一个猜词程序。我这样做是为了让终端让你猜一猜,但当你回答时,它会问你第二次。为什么会发生这种情况,我如何才能修复它?


I tried using variables instead of functions, and I tried looking up solution, but I just can’t find any.

我试着用变量而不是函数,我试着查找解决方案,但我就是找不到。


import random

def guess():
x = input("What's your guess? ")
return x

def anslet():
q = answer[0]
return q

def glet():
w = guess()[0]
return w

answers = ["mouse", "sound", "apple"]
answer = random.choice(answers)
print(answer)

while guess() != answer:
if glet() == anslet():
print("good")

print("Correct!")

更多回答

@JohnGordon Didn't see that answers and answer were different. My mistake.

@JohnGordon并没有意识到答案和答案是不同的。是我的错。

This carefully about the logic of the code. Where it says w = guess()[0], what does this mean, step by step? When you run the program, how many total times do you expect the guess function to be called? Why? Every time that guess is called, input gets called, right? Do you see how this explains the observed result? Before asking future questions, please read How to Ask and How to debug small programs, and carefully check what the code does, step by step. Stack Overflow does not exist to find bugs, but to explain them.

这仔细考虑了代码的逻辑.这里写着w = guess()[0],这是什么意思,一步一步?当你运行这个程序时,你希望guess函数总共被调用多少次?为什么?每次调用guess,都会调用input,对吧?你明白这是如何解释观察到的结果的吗?在提出未来的问题之前,请阅读如何提问和如何调试小程序,并一步一步地仔细检查代码的功能。Stack Overflow的存在不是为了发现bug,而是为了解释它们。

优秀答案推荐

while guess() != answer:
if glet() == anslet():
print("good")

guess() asks the user for input.

Guess()要求用户输入。


glet() calls guess().

Glet()调用Guess()。


So, guess() is called once in the first if condition, and again in the second if condition...

因此,guess()在第一个if条件中被调用一次,在第二个if条件中又被调用一次。



The reason your program is asking for the guess twice is because you are calling the guess() function multiple times within your while loop. Each time you call guess(), it prompts the user for input. So, when you call it in the while condition and then again inside the loop, it results in two separate prompts for input.

您的程序两次请求猜测的原因是因为您在While循环中多次调用Guess()函数。每次调用Guess()时,它都会提示用户输入。因此,当您在While条件中调用它,然后在循环中再次调用它时,它会导致两个单独的输入提示。


Try this:

试试这个:


guessed_word = guess()  # Call guess() once and store the result in a variable

while guessed_word != answer:
if glet() == anslet():
print("good")
guessed_word = guess() # Ask for the next guess

print("Correct!")


Your program calls for the function "guess()" in the while's condition (first input), and afterwards it calls for the function "glet()" (that calls the function "guess()") in the if's condition (second input).

您的程序在While的条件(第一个输入)中调用函数“Guess()”,然后在IF的条件(第二个输入)中调用函数“Glet()”(调用函数“Guess()”)。


def guess():
x = input("What's your guess? ") # here is the input of the function "guess()"
return x

...

def glet():
w = guess()[0] # and here the function "glet" calls for the function "guess()"
return w


...

while guess() != answer: # first input
if glet() == anslet(): # second input
print("good")

print("Correct!")

更多回答

glet() still calls guess() again.

Glet()仍然会再次调用Guess()。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com