简体   繁体   English

如果 boolean function 调用的条件在 Python 中始终为 True

[英]If condition with boolean function call is always True in Python

I have written a program and at the end I have written a code for the user to rate my program, but it has some issues please help:我写了一个程序,最后我写了一个代码供用户评价我的程序,但它有一些问题请帮忙:

while True:
    RR = input("What would you rate this program(?/5): ")
    if RR.isnumeric:
        rating = int(RR)
        if rating >= 5:
            print("Looks like you are highly satisfied with this program :D")
            break
        elif rating == 4 or rating == 3:
            print("Ohh! Next time I'll try my best to change this '",rating,"' into 5 ;D")
            break
        elif rating == 1 or rating == 2:
            print("I am sorry if I wasn't good, I'll try my best next time :|")
            break
    else:
        print("Invalid Rating, Try again...")
        continue

Result结果

What would you rate this program(?/5): g
ValueError: invalid literal for int() with base 10: 'g'

What I want is that if someone enters a text instead of a number then it tells that it's an invalid input and continues the loop.我想要的是,如果有人输入文本而不是数字,那么它会告诉它是无效输入并继续循环。 How can I get this?我怎样才能得到这个?

Your if condition is not a function call, but rather a reference to the function.您的 if 条件不是 function 调用,而是对 function 的引用。 It is not yet called.它还没有被调用。

It should be if RR.isnumeric() not if RR.isnumeric .应该是if RR.isnumeric()而不是if RR.isnumeric

Your code has two problems.您的代码有两个问题。

  1. Your if condition checks str(RR).isnumeric which is always True, since that function exists for all string objects.您的 if 条件检查str(RR).isnumeric始终为真,因为所有字符串对象都存在 function 。 Change it to RR.isnumeric() .将其更改为RR.isnumeric()

  2. In your inner if you might want to put in an else:contine to not bypass the voting system, when a value smaller than 1 is entered.在你的内部,如果你可能想要输入一个else:contine来不绕过投票系统,当输入一个小于 1 的值时。

Here's code that should do what you want:这是应该做你想做的代码:

while True:
    RR = input("What would you rate this program(?/5): ")
    if RR.isnumeric():
        rating = int(RR)
        if rating >= 5:
            print("Looks like you are highly satisfied with this program :D")
            break
        elif rating in [3, 4]:
            print("Ohh! Next time I'll try my best to change this '",rating,"' into 5 ;D")
            break
        elif rating in [1, 2]:
            print("I am sorry if I wasn't good, I'll try my best next time :|")
            break
        else:
            print("Invalid Rating, Try again...\nPlease enter a value from 1 to 5")
    else:
        print("Invalid Rating, Try again...\nPlease enter a value from 1 to 5")

You should tell the user what they should do imo, and you don't need a continue in a while True if you don't need to skip any code anyway.你应该告诉用户他们应while True ,如果你不需要跳过任何代码,你暂时不需要continue Also a list comparison to check if your rating is within a set of numbers is nicer to read.此外,检查您的评分是否在一组数字内的列表比较更易于阅读。 Another very enjoyable way to compare ranges in Python is like this:在 Python 中比较范围的另一种非常有趣的方法是这样的:

if 0 < x < 7:
    pass
elif 6 < x < 10:
    pass

But for small ranges like in your case, I prefer using lists.但是对于像你这样的小范围,我更喜欢使用列表。

HTH HTH

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

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