total=0
def readAndTotal():
global total
num=float(input('Enter a number'))
if num !='':
total+=num
return readAndTotal()
else:
return total
total = readAndTotal()
print("The total of all those values is", total)
A program that outputs the sum of input values before pressing the Enter key. When you activate this code, it says that the str value cannot be converted to the float value. Which part is the problem?
ValueError: could not convert string to float:
num=float(input('Enter a number'))
if num !='':
total+=num
return readAndTotal()
Do not switch to float as soon as input is received.
num=input('Enter a number')
if num !='':
total+=float(num)
return readAndTotal()
First, you need to receive a string input, check if the input is an empty string, and replace it with float when it is not.
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
610 GDB gets version error when attempting to debug with the Presense SDK (IDE)
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
911 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.