While循环并在python中调用函数

def func(val):


    num = int(input("Enter a number:"))

    while(num!=val):

        if num < val:

            print ("Too low!")

            return -1

            num = int(input("Try again: "))

        elif num > val:

            print ("Too high!")

            return 1

            num = int(input("Try again: "))

        else:

            print ("Got it!!!")

            break

            return 0

        print

    func(20)

使用此代码,它不会循环。它会询问我的号码是多少,并让我知道它是否正确,但它不会重新要求我输入新号码。我是否在不正确的地方调用了该函数?还是我的 while 循环中的条件不正确?


皈依舞
浏览 259回答 5
5回答

慕码人2483693

您可以尝试将“num”行放入 while 循环中。

有只小跳蛙

首先,断线退出循环,在你的情况下,当循环退出时,函数也退出。return -1语句退出函数,所以如果你想获得用户的输入,无论用户第一次输入什么等等,你都不应该使用它。正确的代码将如下所示。def func(val):&nbsp;&nbsp; &nbsp; num = int(input("Enter a number:"))&nbsp;&nbsp; &nbsp; while(num!=val):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if num < val:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Too low!")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = int(input("Try again: "))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; elif num > val:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Too high!")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = int(input("Try again: "))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Got it!!!")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0&nbsp;func(20)PS Identation 在 Python 中非常重要,因此请正确格式化您的代码。

qq_花开花谢_0

这是你想要的?def func(val):&nbsp; while True:&nbsp; &nbsp; &nbsp; num = int(input("Enter a number:"))&nbsp; &nbsp; &nbsp; if num < val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Too low!")&nbsp; &nbsp; &nbsp; elif num > val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Too high!")&nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Got it")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; breakfunc(20)

RISEBY

倒数第二行打印不可用,这是正确的代码:def func(val):num = int(input("Enter a number:"))while(num!=val):&nbsp; &nbsp; if num < val:&nbsp; &nbsp; &nbsp; &nbsp;print ("Too low!")&nbsp; &nbsp; &nbsp; &nbsp;return -1&nbsp; &nbsp; &nbsp; &nbsp;num = int(input("Try again: "))&nbsp; &nbsp; elif num > val:&nbsp; &nbsp; &nbsp; &nbsp;print ("Too high!")&nbsp; &nbsp; &nbsp; &nbsp;return 1&nbsp; &nbsp; &nbsp; &nbsp;num = int(input("Try again: "))&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp;print ("Got it!!!")&nbsp; &nbsp; &nbsp; &nbsp;break&nbsp; &nbsp; &nbsp; &nbsp;return 0func(20)

慕尼黑的夜晚无繁华

It will never ask you the number again, because you are returning a value in every condition before asking for a number. So it gets out of the loop at the very first time.# the function can be like thisdef func(val):&nbsp; while True:&nbsp; &nbsp; &nbsp; num = int(input("Enter a number:"))&nbsp; &nbsp; &nbsp; if num < val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Too low!")&nbsp; &nbsp; &nbsp; elif num > val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("Too high!")&nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Got it")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; breakfunc(20)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python