• Register
1 vote
69 views

Problem:

I want to create a python script that should take input from the user and compare the value with zero. Every time the script should print a positive integer number. It doesn’t matter what the user has given as an input. Let’s say the user has given -5 as an input but the output should be 5. I guess it is not something that difficult to do. I tried to write a script to do so but the script is not printing as I expected and throws back an error says bad operand type for unary -: 'str'. I am attaching my sample script below. Please, have a look and help me to fix it.

n = input('input a random number: ')
if int(n) >=0:
    print(n)
else:
    print(-n)

I am a novice in python programming. So please keep your solution as simple as possible. I appreciate your help a lot.
Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

The problem happened with the very first line of your script. In your case, you are taking input as a string, because you didn't initialize n as an integer type. Then, in the next line you expecting the input as an integer value. That is really absurd. To avoid the TypeError take the input as an integer and stored to your variable n and then proceed on.

n = int(input('Input a random number: '))
if n >=0:
    print(n)
else:
    print(-n)

Now, the script is looking good and should print as you are expecting.

Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 12 views
12 views
Problem: I cannot figure out a problem I am having with code written in Python 2.7. I am converting the references to ints, but I keep getting a type exception bad operand type for unary +: 'str'. Can anyone assist?
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 41 views
41 views
Problem: I cannot figure out a problem I am having with code written in Python 2.7. I am hitting the operand exception bad operand type for unary +: 'str' when it gets to if int(splitLine[0]) > int(lastUnix): even though both values being compared print out as ints when tested.
asked Feb 24 Mashhoodch 13k points
0 votes
1 answer 92 views
92 views
Problem: typeerror: unsupported operand type(s) for +: 'nonetype' and 'str'
asked Feb 15 Ethan ross 2.7k points
1 vote
1 answer 100 views
100 views
Problem: I wrote the following program in python and it throws me the error message: Please enter your first number: 10 Please enter your secoend number: 5 Addition of your numbers 105 Traceback (most recent call last): File "ires.py", line 9, in <module> ... some wong output and then it throws the error message. Is there any problem in my program thats causing this error? Thanks in advance.
asked May 12, 2020 samhaz 5k points
0 votes
1 answer 10 views
10 views
Problem: I wrote rock-paper-scissor game with python and I have error which I can't solve. please help. import random p1 = random.randint(0, 2) def choosing(): p = int(input("Rock: 0; Paper: 1; scissor: 2:")) if p < 0 or p > 2: print("\ ... , in <module> TypeError: unsupported operand type(s) for -: 'NoneType' and 'int' and error comes only when I input valid value after entering invalid value
asked May 3 jamuna1 30.2k points
1 vote
1 answer 151 views
151 views
Problem: For the following python program et some error messages. The code: def Yanwa(Array,a): n = len(Array) for column in xrange(n-1): for row in xrange(column+1, n): xmult = Array[row][column] / Array[column][column] Array[row][column] = xmult for col in ... not defined Typeerror: unsupported operand type(s) for -: 'list' and 'list' Can any one sort this problem and help me out to solve this?
asked Apr 27, 2020 samhaz 5k points
1 vote
1 answer 688 views
688 views
Problem: I have been working on project. For that I wrote the code: def For_me(Array,a): n = len(Array) for column in xrange(n-1): for row in xrange(column+1, n): xmult = Array[row][column] / Array[column][column] Array[row][column] = xmult for col in xrange( ... [7]]) But it gives me error message: TypeError: unsupported operand type(s) for -: 'list' and 'list' Can anyone help me otto solve this?
asked Apr 5, 2020 LizzyM 6.1k points
1 vote
1 answer 240 views
240 views
Problem : I have recently started learning Python 3. I am beginner to the programming world. Please find below the code responsible for the error: a = "I know the %d types of people." % 10 Integer = "integer" do_not = "don't" b = "Those type of people ... below error: &ldquo;Typeerror: unsupported operand type(s) for %: 'nonetype' and 'tuple'&rdquo;. Kindly guide me in fixing my above error?
asked Apr 2, 2020 morrisBson 3.2k points
0 votes
1 answer 5 views
5 views
Problem: How can I concentrate unsupported operand type(s) for +: 'int' and 'str' ?
asked Apr 13 sumaiya simi 43.9k points
0 votes
1 answer 39 views
39 views
Problem: I am new to Python and am stuck with what to do right now because I keep getting this error: Unsupported operand type(s) for +: 'float' and 'str' I appreciate all the help I can get. Thanks a lot
asked Mar 16 ummesalma 29.2k points