我正在创建一个(非常粗糙的)计算器,想知道这段代码有什么问题
I'm creating a (very crude) calculator and would like to know what's wrong with this code
# Pick the numbers you'd like to use
number1 = (input('What is the first number?\n'))
number2 = (input('What is the second number?\n'))
# Choose what to do with those numbers
Choice = input('Would you like to add, subtract, multiply or divide?\n ')
# Where the calculating happens, if it doesn't go through the error message asking you to use numbers will happen
try :
# Converting the numbers from str into float, if this doesnt happen it means numbers were not used
num1 = float(number1)
num2 = float(number2)
if Choice is add :
answer = num1 + num2
elif Choice is subtract :
answer = num1 - num2
elif Choice is divide :
answer = num1/num2
elif Choice is multiply
answer = num1 * num2
print(answer)
# The error message if the numbers you gave werent in numeric form
except :
print('Please choose proper numbers in numeric form instead of letter/words')
这是代码,我遇到的问题是:
'文件“main.py”,第 13 行
elif 选择是 subtract :
^
语法错误:语法无效'
如有任何帮助,我们将不胜感激,谢谢 :)。 (如果这段代码根本不起作用,请 lmk。我正在阅读一本关于如何编码的书,我认为尝试这会很有趣,因为我刚刚了解了布尔值和变量)
我想你的意思是:
if Choice == 'add':
answer = num1 + num2
elif Choice == 'subtract':
answer = num1 - num2
elif Choice == 'divide':
answer = num1/num2
elif Choice == 'multiply':
answer = num1 * num2
注意缩进。如果你有一个单独的 if / elseif / elseif / elseif / else 链,每个条件都应该处于相同的缩进级别,并且每个主体都应该匹配它的条件加上一个缩进(通常是 4 个空格或 1 个制表符)。
要将使用 input()
从用户那里捕获的字符串与文字字符串进行比较,您可以这样做:
if Choice == 'add':
您需要在 'add'
周围指定引号,否则它会尝试引用名为 add
的变量,该变量未定义。
对于 ==
与 is
检查字符串时,请参阅 Why does comparing strings using either '==' or 'is' sometimes produce a different result?
# Pick the numbers you'd like to use
number1 = (input('What is the first number?\n'))
number2 = (input('What is the second number?\n'))
# Choose what to do with those numbers
Choice = input('Would you like to add, subtract, multiply or divide?\n ')
# Where the calculating happens, if it doesn't go through the error message asking you to use numbers will happen
try :
# Converting the numbers from str into float, if this doesnt happen it means numbers were not used
num1 = float(number1)
num2 = float(number2)
if Choice is add :
answer = num1 + num2
elif Choice is subtract :
answer = num1 - num2
elif Choice is divide :
answer = num1/num2
elif Choice is multiply
answer = num1 * num2
print(answer)
# The error message if the numbers you gave werent in numeric form
except :
print('Please choose proper numbers in numeric form instead of letter/words')
这是代码,我遇到的问题是:
'文件“main.py”,第 13 行 elif 选择是 subtract : ^ 语法错误:语法无效'
如有任何帮助,我们将不胜感激,谢谢 :)。 (如果这段代码根本不起作用,请 lmk。我正在阅读一本关于如何编码的书,我认为尝试这会很有趣,因为我刚刚了解了布尔值和变量)
我想你的意思是:
if Choice == 'add':
answer = num1 + num2
elif Choice == 'subtract':
answer = num1 - num2
elif Choice == 'divide':
answer = num1/num2
elif Choice == 'multiply':
answer = num1 * num2
注意缩进。如果你有一个单独的 if / elseif / elseif / elseif / else 链,每个条件都应该处于相同的缩进级别,并且每个主体都应该匹配它的条件加上一个缩进(通常是 4 个空格或 1 个制表符)。
要将使用 input()
从用户那里捕获的字符串与文字字符串进行比较,您可以这样做:
if Choice == 'add':
您需要在 'add'
周围指定引号,否则它会尝试引用名为 add
的变量,该变量未定义。
对于 ==
与 is
检查字符串时,请参阅 Why does comparing strings using either '==' or 'is' sometimes produce a different result?