计算输入的abs()值 - Python

时间:2015-09-25 17:52:13

标签: python

我刚刚开始学习编程,现在为了学校作业,我们必须在Python中创建一个程序,要求用户输入一个整数,然后计算它的绝对值。我知道abs函数。我无法弄清楚如何将用户输入分配给abs函数。

我会写:

a = int (raw_input ("give your number")) 
int = abs()

学校作业:

编写一个计算数字绝对值的程序。也就是说,-15的绝对值是15,绝对值10是10等等。

3 个答案:

答案 0 :(得分:3)

你想做点什么吗? a = math.abs(a)? 这会将a分配给a。的绝对值。

完整代码:

import math
a = int(input("Input a number"))
a = math.abs(a)
print(a)

答案 1 :(得分:1)

>>> a = -5
>>> a if a > 0 else -a
5

答案 2 :(得分:1)

我明白了。

print "Welcome to the absolute number calculator."
while True:
     print "Plz give a number you would like to calculate then hit enter."
     a = (input())
     int(a)
     print "Your absolute number is:"
     print (abs(a))
     print"would you like to calculate another number?"
     print"press n then enter if you want to stop, press y then enter to continue"
    b = (raw_input())
    if b == "n":
        break
相关问题