简体   繁体   English

Python if语句在函数内部不起作用

[英]Python if statement not working inside function

I'm trying to make a guess the number game in python. 我正在尝试猜测python中的数字游戏。 However, in the again() function I cannot seem to get the if statement to run. 但是,在again()函数中,我似乎无法运行if语句。

When I don't use the again function and copy and paste all the code in the respective positions, it works fine. 当我不使用Again函数并将所有代码复制并粘贴到相应位置时,它可以正常工作。 However when I use the again function, the "play again?" 但是,当我使用Again函数时,“是否再次播放?” question is asked but the if statement is ignored, causing the while loop to continue endlessly. 询问问题,但if语句被忽略,导致while循环不断地继续。 I have tried using the global function but on the second time I input a guess, it comes up with TypeError: 'str' object is not callable . 我尝试使用全局函数,但是第二次输入一个猜测,它带有TypeError:'str'对象不可调用。

import random

def guess():
    global num
    num = random.randint(1,3)
    guessat  = input("Out of 1 to 3, which number do you guess? ")
    return(guessat)

def again():
    global again
    again = input("Play again? ")
    if again in ["y","yes"]:
        guessing = True
    else:
        guessing = False

print("Welcome to Guess the Number!")

guessing = True

while guessing:
    guessy = guess()
    guessy = int(guessy)
    if guessy == num:
        print("You got it right!")
        again()

    elif guessy != num:
        print("Wrong number!")
        again()
quit()

When I input "no" or anything else for the question "Play Again?" 当我为问题“再次播放?”输入“否”或其他任何内容时 I expect the program to exit the while loop and quit. 我希望程序退出while循环并退出。

The if/else statement works perfectly, the problem is elsewhere. if / else语句可以完美运行,问题出在其他地方。 More specifically, there are 2 things wrong in your code. 更具体地说,您的代码中有两处错误。

  1. the use of the same name again for both the variable and the function, you should use different names. 如果变量和函数again使用相同的名称,则应使用不同的名称。

  2. the guessing variable should be global as well otherwise the while-loop will never see it changing. guessing变量也应该是全局变量,否则while循环将永远不会看到它的变化。

Try this: 尝试这个:

def again():
    global guessing
    _again = input("Play again? ")
    if _again in ["y","yes"]:
        guessing = True
    else:
        guessing = False

One more thing. 还有一件事。 As others commented already, the use of global variables is generally not a good idea, is better to have your function return something instead. 正如其他人已经指出的那样,使用全局变量通常不是一个好主意,最好是让您的函数返回某些内容。

Avoid naming your variable again inside a function called again . 避免在again调用的函数中again命名变量。 However in regards to your infinite loop problem, you set local variable guessing inside the again function, no the global variable guessing , hence the variable checking the while loop condition is not affected at all. 但是,对于您的无限循环问题,您可以在again函数内设置局部变量guessing ,而无需全局变量guessing ,因此检查while循环条件的变量完全不受影响。 Might I suggest: 我可能建议:

def again():
    global guessing
    play_again_input = input("Play again? ")
    if play_again_input in ["y","yes"]:
        guessing = True
    else:
        guessing = False

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

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