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函数?为什么?每次调用该猜测时,输入都会被调用,对吗?你明白这如何解释观察到的结果了吗?在提出以后的问题之前,请阅读如何提问和如何调试小程序,并仔细检查代码的作用,一步一步地。堆栈溢出的存在不是为了寻找错误,而是为了解释它们。
优秀答案推荐
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...
因此,在第一个If条件中调用一次Guess(),在第二个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()。
我是一名优秀的程序员,十分优秀!