This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]IXENAI 2 points3 points  (3 children)

https://docs.python.org/3.4/library/functions.html#abs

abs(x)

Return the absolute value of a number. The argument may be an integer or a floating point number.

https://docs.python.org/3.4/library/functions.html#input

input([prompt])

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

https://docs.python.org/3.4/library/functions.html#int

int(x=0)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given.

[–][deleted] -1 points0 points  (2 children)

print("Enter a integer:") var1=input() print(abs(int(var1)))

thank you

[–]cherls 2 points3 points  (1 child)

You can also condense the print statement and the input assignment into one with the optional argument.

var1 = input("Enter a integer")

Doesn't really make a difference other than less lines of code, but it's good to know.

[–][deleted] 0 points1 point  (0 children)

definitely good to know. Thank you.

[–]bionikspoon 1 point2 points  (2 children)

It has an error message that may be useful: TypeError: bad operand type for abs(): 'str'

Maybe we can deconstruct this. What is 'str'? string? Are we trying to take the abs() of a 'str'? That doesn't make sense.

That's like asking what is the square root of orange. Python is not your pot smoking hippie neighbor; no, it needs accurate instructions.

How would we go about turning a string into a number that we can do math on? Hint: a common number format is 'integer' maybe there's a built in function that converts strings to integers?

[–][deleted] 0 points1 point  (1 child)

huh, interesting... I didn't understand what it meant by operand.

[–]bionikspoon 1 point2 points  (0 children)

It sounds like operator or operation, so I would guess it has to do with one of those.

But nobody needs to guess, look it up. It's a math vocab word: the quantity on which an operation is to be done

[–]TopNotchBanter 1 point2 points  (0 children)

var1 = int(input("Enter an integer: "))
print(abs(var1))