203k views
0 votes
Why is this python code giving me problems?

This is having the user input a decimal number and the code has to round it up to the 2nd decimal place. This code is giving me problems, please fix it.

num3 = int(input("Please input a decimal number:")

num3 = int(round(num3, 2))

print ("your decimal rounded to the 2nd decimal place is:", x)

User Carlene
by
4.7k points

1 Answer

4 votes

Answer:

The answer to this question is given below in the explanation section.

Explanation:

The given code in this program has syntax errors.

In the given code, at line 1, input will cast or convert to int. It will generate an error on the second line because integer numbers can't be rounded. In simple, integer numbers don't have decimals. So, to correct the line you must use float instead of int.

In the second line, you need also to emit the int casting (data type conversion), because you have already converted the input into the float. In line 3, the second parameter to print function is num3, not x.

So the correct lines of the python code are given below:

num3 = float(input("Please input a decimal number:"))

num3 = (round(num3, 2))

print ("your decimal rounded to the 2nd decimal place is:", num3)

When you will run the above bold lines of code, it will run the program successfully without giving you any syntax and semantic error.

User Assassin
by
5.4k points