python中的加减法

2024-03-29 11:06:54 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我做了一个statcalc,除了添加之外,一切都正常。当我选择要添加的选项时,只会跳过它并说选择一个选项。我在想怎么了?

numberstoadd = input("What is the first number you want to add? ")
numbertoadd = input("What do you want to add to it? ")
sum = numbertoadd + numberstoadd
print sum

Tags: thetoyouaddnumberinputis选项
3条回答

您需要将输入strings转换为ints

number_1 = int(raw_input("What is the first number you want to add? "))
number_2 = int(raw_input("What do you want to add to it? "))
sum = number_1 + number_2
print sum

在Python 2中,inputeval类型化文本并返回整数,而在Python 3下,input只返回包含类型化文本的string(相当于python2中的raw_input)。

有关Python版本2.x&3.x之间的其他更改,请参见此链接

http://docs.python.org/dev/whatsnew/3.0.html

print("Welcome to fizz buzz")
num1=input("Choose a number from 1 to 100")
if num1 is >= 50:
 print("hello")
else:
 print("good bye")

相关问题 更多 >