如何在循环下检查整数?

fqas = None

while fqas not in ("yes", "no", "Yes", "No"):

    fqas = input(">>> [yes/no]: ")

    if fqas.lower() == "yes":

         print("\nAwesome! Current pay: $", base_pay + 50)

    elif fqas.lower() == "no":

        fqas_no = int(input("if not, how much do you think she deserve? \n>>> "))

        if 50 <= fqas_no :

            print("AMAZINGGGG! current pay: $", base_pay + fqas_no)

            continue

        elif fqas_no <= 50 :

            print("That's cool, current pay: $", base_pay + fqas_no)

            continue

        except ValueError:

            print("Numbers only please")

如何添加最后一个循环以检查下的整数fqas_no?我已经检查了范围,但是我无法让它检查整数。SyntaxError当我尝试使用时,我得到一个无效的信息ValueError。


该代码可以正常工作,除了:


        except ValueError:

             print("Numbers only please")


慕田峪7331174
浏览 178回答 3
3回答

HUH函数

在获得有效(或无效)输入之前,您已经知道如何循环。要检测整数,您可以尝试将输入转换为inttry:&nbsp; &nbsp; value = int(fqas_no)except:&nbsp; &nbsp; # loop around to try again更好的是,使用内置方法if fqas_no.isdigit():

蝴蝶不菲

查看有关异常处理的文档。您需要包含一条try声明:try:&nbsp; &nbsp; if 50 <= fqas_no :&nbsp; &nbsp; &nbsp; &nbsp; #etcexcept ValueError:&nbsp; &nbsp; print("Numbers only please")这两个在一起。只是except不正确的语法。旁注,您应该调用lower()输入本身。这样,您的代码将更加简洁:while fqas not in ["yes", "no"]:&nbsp; &nbsp; fqas = input(">>> (yes/no): ").lower()&nbsp; &nbsp; if fqas == "yes":&nbsp; &nbsp; &nbsp; &nbsp; #etc

郎朗坤

没有尝试的除外部分似乎是您的问题。base_pay = 1000fqas = Nonewhile fqas not in ("yes", "no", "Yes", "No"):&nbsp; &nbsp; fqas = input(">>> [yes/no]: ")&nbsp; &nbsp; if fqas.lower() == "yes":&nbsp; &nbsp; &nbsp; &nbsp; print("\nAwesome! Current pay: $", base_pay + 50)&nbsp; &nbsp; elif fqas.lower() == "no":&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; fqas_no = int(input("if not, how much do you think she deserve? \n>>>"))&nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; print('Numbers only please')&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; if 50 <= fqas_no:&nbsp; &nbsp; &nbsp; &nbsp; print("AMAZINGGGG! current pay: $", base_pay + fqas_no)&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; elif fqas_no <= 50:&nbsp; &nbsp; &nbsp; &nbsp; print("That's cool, current pay: $", base_pay + fqas_no)&nbsp; &nbsp; &nbsp; &nbsp; continue这应该可以解决您的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python