Python计算器程序输出频繁出现None问题求助
问题修复方案
问题根源
你代码里的input(print("Enter the first value "))写法错误。print()函数执行后返回的是None,当你把它作为input()的参数时,input()会把这个None当作提示文本输出,这就是你看到多余None的原因。input()本身支持直接传入提示字符串,不需要嵌套print()。
修复后的代码
num1 = float(input("Enter the first value ")) num2 = float(input("Enter the second value ")) operator = input("Enter the operator you want to calculate with;+,-,* ") sum_result = num1 + num2 product = num1 * num2 difference = num1 - num2 if operator == "+": print(sum_result) elif operator == "*": print(product) else: print(difference)
注:这里把变量名
sum改成了sum_result,因为sum是Python内置函数,用它当变量名可能会引发后续问题。
修复后输出示例
Enter the first value 1 Enter the second value 2 Enter the operator you want to calculate with;+,-,* + 3.0
内容的提问来源于stack exchange,提问作者Syed Shah
相关产品推荐
相关产品推荐

