社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python if语句不能在函数内部工作

kenbinner • 5 年前 • 1424 次点击  

当我不使用again函数并在相应位置复制和粘贴所有代码时,它可以正常工作。不过,当我使用“再次播放”功能时,“再次播放?”问题被提出,但if语句被忽略,导致while循环无休止地继续。我试过使用global函数,但在第二次输入guess时,它出现了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()

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53845
 
1424 次点击  
文章 [ 2 ]  |  最新文章 5 年前