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
257
Reaction score
35
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
156
Reaction score
22
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
463
Reaction score
56
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
131
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) )
 
Joined
May 14, 2024
Messages
1
Reaction score
0
It looks like you want the output to have only one decimal place of precision. To achieve this, you can use the round() function to round the precio variable to one decimal place before printing it. Here's the modified code:

Python:
x = int(input())

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

precio = x * (1 - y)
precio = round(precio, 1)  # Round to one decimal place
print(precio)

With this modification, the output for the last testcase (199) will be 149.3, which matches your expected output.

Additionally, if you need further help with Python assignment or encounter any more coding challenges, there are resources available online where you can find guidance and support. Exploring programming forums or seeking help from online communities can often provide valuable insights and solutions like ProgrammingHomeworkHelp.com. Remember, learning to troubleshoot and debug code is an essential skill for any programmer. You might also consider reaching out to professional services specializing in programming homework assistance to get personalized help with your assignments.
 

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,919
Messages
2,570,037
Members
46,444
Latest member
MadeleineH

Latest Threads

Top