简体   繁体   English

我刚刚开始在 Python 中制作一个基本的交互式计算器,但无法弄清楚为什么它不接受给定的输入

[英]I have just started making a basic interactive calculator in Python, but can't figure out why it won't accept the given input

num = input("Please enter one number to be added:")
num1 = input("Please enter another number to be added:")
num2 = input("Please enter the third number to be added (if no more numbers are required, type None:")
if num2 == "None" or "NONE" or "none" or "nOne":
    print("One moment please")
    print(num * num1)
else: 
    print("Maximum Numbers Reached")

Above is a sample of the code.以上是代码示例。 The line with num * num1 is the one that has been giving me trouble.带有 num * num1 的那一行一直给我带来麻烦。 I've tried several different variations, including str, int, float, but they won't multiply (or add, subtract, etc...).我尝试了几种不同的变体,包括 str、int、float,但它们不会相乘(或加、减等)。 I've changed everything from the input method to making numbers strings and vice versa.我已经改变了一切,从输入法到制作数字字符串,反之亦然。 I've tried debugging(didn't work) but nothing makes sense.我试过调试(没有用)但没有任何意义。 I made a separate file and ran this:我制作了一个单独的文件并运行了这个:

me = 2
me2 = 3
print(me + me2)

This adds me and me2 just fine.这添加 me 和 me2 就好了。 However, the original section just won't work.但是,原始部分将不起作用。 Is there something wrong here, or do I need to give my brain some recharge time...?这里有什么问题吗,还是我需要给大脑充电时间……? Anyway, if you have any ideas, I would appreciate the help.无论如何,如果您有任何想法,我将不胜感激。 Thanks, Jerry谢谢,杰里

There are a few issues in the code, refer to the modified code -代码中有几个问题,参考修改后的代码——

check = True # condition for invalid input
while check:
    try:
        num = int(input("Please enter one number to be added:")) # needs to be type int
        num1 = int(input("Please enter another number to be added:")) # needs to be type int

        # not a good approach to accept None inplace of a number
        num2 = int(input("Please enter the third number to be added (if no more numbers are required, Enter 0")) 
    
        check = False 
    except:
        print('Please enter only Integer values')

if not num2:
    print("One moment please")
    print(num + num1) # you are multiplying while saying taking numbers for addition? code has been edited here to addition '+'
else: 
    print("Maximum Numbers Reached")

you had a quite common beginner mistake!你有一个很常见的初学者错误! basically what you did was instead of基本上你所做的是而不是

variable = int(input(words))

you put你把

variable = input(words)

you need the int at the beginning or everything is seen as a string.您需要在开头使用 int ,否则所有内容都被视为字符串。

When you are inputting numbers like this, the numbers are taken as strings not integers:当您像这样输入数字时,数字将被视为字符串而不是整数:

Try this:尝试这个:

num1 = int(input("Please enter one number to be added:"))
num2 = int(input("Please enter another number to be added:"))
choice = input("Please enter the third number to be added (if no more numbers are required, type None:").lower()
if choice == 'none':
    print("One moment please")
    print(num1 * num2)
else: 
    print("Maximum Numbers Reached")

Edit: If condition modified编辑:如果条件修改

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

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