-2

I'm trying to use an input function in python and want to format how the input actually displays in python.

The code is currently:

price = float(input("Enter value: "))

and prints out the following if I input 9.0 (note that python does this automatically without using a print statement).

Enter value: 9.0

How do I format the output so that it only shows the value with 0 decimal points i.e., should print out

Enter value: 9

Using a print() statement and {:,g} is not an option in this exercise.

Gary Kong
  • 45
  • 1
  • 5
  • 1
    When is the print happening? After you press enter after inputting 9.0? – Richard K Yu Jan 12 '22 at 03:49
  • 1
    To be clear, "Enter value: " is the prompt, and 9.0 is what you entered, but your code isn't meant to display "Enter value: 9.0" a second time after you've already provided the input? – VentricleV Jan 12 '22 at 03:53
  • The `print()` function prints 9.0 as 9.0 to show that it's a floating point value and not an integer. You *can* format it so that it doesn't show any decimals, but you have to ask yourself if you really know better than what most programming languages do and what almost any user will expect as a result. – Grismar Jan 12 '22 at 03:53
  • you can format your output while printing using formating string in the print statement it self – Deven Ramani Jan 12 '22 at 03:54
  • Does this answer your question? [Formatting floats without trailing zeros](https://stackoverflow.com/questions/2440692/formatting-floats-without-trailing-zeros) – Grismar Jan 12 '22 at 04:19
  • Hi Grismar. Unfortunately not. I'm aware of how to format the output using a print statement (using string formatting). Whenever we make a call to the input() function in python, Jupyter notebooks will always print out the input. – Gary Kong Jan 12 '22 at 05:47
  • What I need to achieve is to change how jupyter notebook prints out the input without actually calling print() – Gary Kong Jan 12 '22 at 05:48
  • @RichardKYu the code doesn't use a print() statement as input() will automatically output whatever is inputted – Gary Kong Jan 12 '22 at 05:54
  • It doesn't "output" whatever is inputted. Whatever you input is displayed on the screen... Just don't input the `.0` part... – Tomerikoo Jan 12 '22 at 08:05

1 Answers1

0

You can modify your code as:

price = float(input("Enter value: "))
price = int(price)
print(price)

This will convert float into an integer.

enggPS
  • 686
  • 1
  • 7
  • 23