Problem with code

Joined
Apr 20, 2024
Messages
2
Reaction score
0
Hi, I have a problem with this code:

x = int(input())

if x <= 100:
y = 0.1
else:
y = 0.25

precio = x * (1 - y)
print(precio)

The code seems fine to me, but I'm not expecting the answer that I wanted in the last testcase of the challenge that my teacher sent me. Here are the inputs and expected outputs:

58 = 52.2
7=6.3
200=150
199=149.3

When I display the code with the last input, the answer is 149.25, which is not what im looking for, what should I do?
 
Joined
Apr 25, 2017
Messages
251
Reaction score
33
You have to round the number to one decimal place
Python:
number = precio

rounded_number = round(number, 1)

print(rounded_number)  # Output will be 149.3
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
Either the program is wrong, or the test data is wrong.

We could check the test data, if we knew what mathematical function the program is trying to implement.

If there is no meaningful function, and the purpose of the program is to output those 4 numbers, given those 4 inputs, then you could write a very simple program with 4 IF statements.
 
Last edited:
Joined
Jul 4, 2023
Messages
368
Reaction score
41
Did you consider to write down in that way?
[ working code on-line ]
Python:
def calcularPrecio(entrada):
    # x = int(input())
    x = entrada
   
    if x <= 100:
        y = 0.1
    else:
        y = 0.25
   
    precio = x * (1 - y)
    precio = round(precio + 0.005, 1)
    precio = int(precio) if precio.is_integer() else precio
   
    print(precio)


numeros = [ 58, 7, 200, 199 ]
for numero in numeros:
    calcularPrecio(numero) # changed to "def" for demonstration purposes
   
'''  
58  = 52.2
7   = 6.3
200 = 150
199 = 149.3
'''
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top