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
254
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
370
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
'''
 
Joined
Sep 4, 2022
Messages
129
Reaction score
16
upgrade :

  • use of 'input parameters' :
  • why using two values ??, equal to 'two memory slots' ? go simple by your function.
entrada == x by your code ,
just throw out 'x'
, keep 'entrada' as only var ( just to have one var for the content value needed ).

-----------------------------------------------
-----------------------------------------------

- build your Maths when constant exists :
you have the formula (1-y) , it's 'computing time' , hardcode can save MIPS.

as you have a constant to use by 1-y ,
you can subsitute the constant by 'const values' , already made,
but a bit more hardcode.
you can use two values : 0.1 // 0.25
it will pass
-----------------------------------------------
-----------------------------------------------

- print with recursive call, and recursive computing :

As Python works on C++ stack and basis , you can go deep with recursion, when operating instructions.

'Print' function can hold more arguments than a 'var' , or strings to be display
print( precio ) could be 'print ( x * (1 - y) )'


Python:
def calcularPrecio(x):

    if x <= 100:
       return x * 0.1
    else:
       return x * 0.25




numeros = [ 58, 7, 200, 199 ]
for numero in numeros:
    print( calcularPrecio(numero) )
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,284
Latest member
NicholeDum

Latest Threads

Top