Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check if number input is empty, using a if statement

the title say it all, i tried doing this:

import random

speed = float(input("Which was the speed of the vehicle in that moment? "))

trafficTicket = 0

if speed == "": 

    print("The speed was not informed. ")

    randomNumber = random.randint(1, 120)

    RandomTrafficTicketValue = randomNumber * 7

    print(f"O value of the traffic ticket in this situation is going to be {RandomTrafficTicketValue} dollars. ")

if speed < 30:

    print("The vehicle's speed is very low, tell the driver to change hands. ")

if speed > 90:

    trafficTicketValue = speed - 90

    print(f"Due to speed above the permitted speed, the driver must receive a trafficTicket worth {trafficTicketValue * 7} dollars. ")

but got this error: ValueError: could not convert string to float: ''

Any way to go around it, so i can make a if statement when the user dont type anything?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i tried finding a solution based in what i needed but only found a question involving python made in 2010, and that dont are especially what im trying to find

>Solution :

Accept the input as a plain string and check if it is empty before you call float():

speed = input("what was your speed? ")

# if they actually typed something, convert it to a float
if speed:
    speed = float(speed)

    # now handle the various speed values

else:
    print("You did not enter anything")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading