python中的加减法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22002635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:03:43  来源:igfitidea点击:

How to add and subtract in python

pythonaddadditionsubtraction

提问by user3337936

So I am making a statcalc and everything is working except adding. When I select the option to add it just skips it and says select an option. I was wondering what's wrong with it?

所以我正在制作一个 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

回答by U2EF1

You need to turn your input strings into ints. Like this:

您需要将输入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

回答by Peter Gibson

In Python 2, inputwould evalthe typed text and return an integer, whereas under Python 3 inputjust returns a stringcontaining the typed text (equivalent to raw_inputin Python 2).

在 Python 2 中,输入的文本inputeval返回一个整数,而在 Python 3 下input只返回一个string包含输入的文本(相当于raw_input在 Python 2 中)。

See this link for other changes between Python version 2.x & 3.x

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

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

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

回答by AKI

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

回答by user12826762

Sample Input 0

样本输入 0

2

2

1 3

1 3

10 100

10 100

Sample Output 0

样本输出 0

-2

-2

-90

-90

Given a two integers print the difference of two integers.

给定一个两个整数,打印两个整数的差。

*Hint: Try to implement without using '-' operator.

*提示:尝试在不使用“-”运算符的情况下实现。