• Register
1 vote
45 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,240 points

Related questions

0 votes
1 answer 5 views
5 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 2 days ago Mashhoodch 9.1k points
0 votes
1 answer 7 views
7 views
Problem: typeerror: unsupported operand type(s) for +: 'nonetype' and 'str'
asked Feb 15 Ethan ross 1.8k points
1 vote
1 answer 68 views
68 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
1 vote
1 answer 76 views
76 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 501 views
501 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 142 views
142 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
1 vote
1 answer 169 views
169 views
Problem: Hi all This is a question from a noob who started coding a few hours back. print("Hello") print("name") print("hello") print("Let's do some math!") print(5 + "a") print("5 + 5") print("Thanks for playing along!") It says, &ldquo;typeerror: unsupported operand type(s) for +: 'int' and 'str'&rdquo; What does it mean?
asked Mar 9, 2020 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
Problem: I created a function to count a letter for example letter e. My function looks something similar to this: def count_letter(sentence, accents, case): lower_case_e = ['e'] upper_case_E = ['E'] accent_lower_case = ['é', 'ê', 'è'] accent_upper_case = ['É', ' ... and depending if it is lower or upper case or if it has accents, to sum the letters together. I created a text file called sentence
asked 4 days ago Mashhoodch 9.1k points
0 votes
1 answer 12 views
12 views
Problem: I've very recently migrated to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code After upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Jan 21 Mashhoodch 9.1k points
0 votes
1 answer 177 views
177 views
Problem: I am running into this error for multiple variables, even handling missing values. For example: le = preprocessing.LabelEncoder() categorical = list(df.select_dtypes(include=['object']).columns.values) for cat in categorical: print(cat) df[cat].fillna('UNK', inplace=True) ... error results in: df['CRM do Médico'].isnull().sum() 0 Apart from nan values, what could be causing this error?
asked Dec 24, 2020 sasha 12.9k points