I was trying to figure out a basic python program with f-strings. The problem was to ask user to enter any amount of ounces and then divide the ounces by 2.34. The output should only be a number with 2 decimal places it can only be 2 lines of code. This was part of my code:
userOunces = input('Enter any amount of ounces to measure something: ')
print(f'This will print out your amount of ounces divided by 2.34: {userOunces/2.34:.2f}')
but it came up with an error? can anybody help me.
CodePudding user response:
You can try
userOunces = float(input("Enter any amount of ounces to measure something: "))
print(f"This will print out your amount of ounces divided by 2.34: {userOunces/2.34:.2f}")
input returns a string so you are required to read the input as a float or int. This allows you to divide the input in your f-string. Use :.2f to round the number to two decimal places.
CodePudding user response:
Thanks for the help on my previous problem! The new solution is -- userOunces = float(input("Enter any amount of ounces to measure something: ")) print(f"This will print out your amount of ounces divided by 2.34: {userOunces/2.34:.2f}") WHY IT WASN'T WORKING BEFORE -- because python returned a string which can not be divided by a number. To fix that you either have to put the int or float in front of the input function so python can divide the user number by 2.34.
