简体   繁体   English

我使用 Python 制作了一个非常基本的脚本(我是新手),我不知道为什么它不工作,因为它应该工作

[英]I made a really basic script using Python (I am new at it) and i don't know why it is not working as it should work

The problem is in the line 19, where the if condition is.问题出在第 19 行,即if条件所在的位置。

So when you run the code it should ask you for a first number, then the mathematical operation (+,plus or -,minus) and finally for the second number.因此,当您运行代码时,它应该要求您输入第一个数字,然后是数学运算(+、加号或 -、减号),最后是第二个数字。

It works perfectly when you are adding (plus), but when you are trying to subtract then it shows you the message "Invalid Operation", i've already tried using other logical operators but it just doesn't work D:当您添加(加号)时它工作得很好,但是当您尝试减去时它会显示消息“无效操作”,我已经尝试使用其他逻辑运算符但它不起作用 D:

Pls explain me what's the problem, 'cause i can't see it.请解释一下有什么问题,因为我看不到它。

minus = ["-","minus"]
plus = ["+", "plus"]

print("""
    ===========================
            CALCULATOR
    ===========================

    1      2      3      +
    4      5      6      -
    7      8      9

    0      Total:
    ===========================
    ===========================
    """)
n1 = int(input("First Number: "))
operation = input("+ or - ")
if operation not in (minus,plus):
    print("Invalid Operation")
else:

    n2 = int(input("Second Number: "))

    if operation in minus:
        total_minus = n1-n2
        print(f"""
    ===========================
            CALCULATOR
    ===========================

    1      2      3      +
    4      5      6      -
    7      8      9

    0      Total: {total_minus}
    ===========================
    ===========================
        """)
    elif operation in plus:
        total_plus = n1 + n2
        print(f"""
    ===========================
            CALCULATOR
    ===========================

    1      2      3      +
    4      5      6      -
    7      8      9

    0      Total: {total_plus}
    ===========================
    ===========================
        """)



The expression operation not in (minus,plus) is testing to see if operation is one of the lists minus or plus in the tuple (minus, plus) .表达式operation not in (minus,plus)正在测试是否operation是元组(minus, plus)中的minusplus列表之一。 Since it's a string, it will never be either of those values.由于它是一个字符串,它永远不会是这些值中的任何一个。

I'd recommend creating a combined list of valid operations.我建议创建一个有效操作的组合列表。

valid_operations = minus + plus # concatenate valid operations

then test to see if the operation that the user input is in that list.然后测试以查看用户输入的操作是否在该列表中。

if operation not in valid_operations:
    print("Invalid Operation")
else:
    ...

This way it's easy to extend your calculator to multiplication, division, etc.这样很容易将您的计算器扩展到乘法、除法等。

You are joining lists by forming a tuple:您正在通过形成一个元组来加入列表:

if operation not in (minus, plus):

What you actually want to do is add the lists together:您真正想要做的是将列表添加在一起:

if operation not in minus + plus:

operation not in (minus,plus) will always be true. operation not in (minus,plus)将始终为真。 operation is a string, and (minus,plus) is a tuple containing two lists. operation是一个字符串, (minus,plus)是一个包含两个列表的元组。

If you want to test if the string operation is in the minus list or the plus list, you could instead use:如果您想测试字符串operation是在minus列表还是plus列表中,您可以改用:

if operation not in (minus + plus):

For me, your code doesn't work with either of + or -!对我来说,您的代码不适用于 + 或 -! I don't know why it works for you with + as I think it shouldn't!我不知道为什么它对你有用 + 因为我认为它不应该!

You are correct that the problem is your if statement.你是正确的,问题是你的 if 语句。 When you write:当你写:

if operation not in (minus,plus):

You are saying "if operation does not equal ["-", "minus"] or ["+", "plus"] "您是在说“如果运算不等于 ["-", "minus"] 或 ["+", "plus"]"

In other words you are comparing the input of the user to a list of 2 strings!换句话说,您正在将用户的输入与 2 个字符串的列表进行比较!

You can instead write:你可以改为写:

if operation not in minus and operation not in plus:

and your program will work just fine你的程序就可以正常工作了

Just change in if block of code只需更改 if 代码块

if operation in plus or operation in minus:
    your code
else:
    print("Invalid Operation")

You wrote:你写了:

if operation not in (minus,plus):
    your code

This will always be True, since operation will never be in both lists (plus and minus) so that statement is False, and since you wrote 'not in', not of False is True, that's the reason you always get "Invalid Operation" in your if block.这将始终为 True,因为operation永远不会在两个列表(加号和减号)中,因此该语句为 False,并且由于您写了 'not in',不是 False 为 True,这就是您总是得到“无效操作”的原因在你的 if 块中。

暂无
暂无

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

相关问题 这个 Python 脚本返回“KeyError:'6'”,我不知道为什么 - This Python script returns “KeyError: '6'” and I don't know why Python脚本崩溃了,我不知道为什么 - Python Script is crashing and I don't know why 我的代码有什么问题? 我是新手,我不知道为什么它不起作用。 (Python) - What is wrong with my code? I am a newbie and I don't know why it isn't working. (Python) python和pygame字体居中工作,但我不知道为什么 - python and pygame font centering is working but I don't know why 为什么这段代码不起作用? 我是新手,我真的需要帮助 - Why isn't this code working? I am new and I really need assistance 我的gridsearchCV不起作用,我也不知道为什么 - My gridsearchCV don't work and i don't know why 如何在 replit.com 中使用 Python 一起打印带有表情符号的文本,我不知道为什么会出现此错误 - How to print a text with an emoji together using Python in replit.com and I don't know why I am getting this error 对 Python 编程有点陌生,正在尝试一个问题。 我只是不知道为什么这个错误不断弹出。 对我来说真的没有意义 - Kinda new to Python Programming and was attempting a question. I just don't know why this error keeps popping up. Really doesn't make sense to me 我正在尝试使用 Python 进行网络抓取,并提出了如下请求并得到了响应。 但不知道如何处理 - I am trying to do web scraping with Python and have made a request like below and got the response. but don't know how to process it 不知道为什么我收到 StopIteration 错误 - Don't know why I am getting StopIteration error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM