Home > database >  ValueError: could not convert string to float: in Python 3.10
ValueError: could not convert string to float: in Python 3.10

Time:06-21

When someone writes a string or a letter, I want the code make them go back, and the code to print "must be a number and bigger than 0 and less than 100", but what actually happens is the code closing. Is there by any chance a fix for this?

import time

def main():
    num1 = input("your t76ely\n")
    num2 = input("your num2\n")
    school = input("your school\n")

    num1 = float(num1)
    num2 = float(num2)
    school = float(school)

    if num1 > 100:
        print("must be a number and bigger than 0 and less than 100")
        while True:
            time.sleep(1)
            main()
    elif num1 < 0:
        print("must be a number and bigger than 0 and less than 100")
        while True:
            time.sleep(1)
            main()
    if num2 > 100:
        print("must be a number and bigger than 0 and less than 100")
        while True:
            time.sleep(1)
            main()
    elif num2 < 0:
        print("must be a number and bigger than 0 and less than 100")
        while True:
            time.sleep(1)
            main()
    if school > 100:
        print("must be a number and bigger than 0 and less than 100")
        while True:
            time.sleep(1)
            main()
    elif school < 0:
        print("must be a number and bigger than 0 and less than 100")
        while True:
            time.sleep(1)
            main()
    else:
        r = (40 % 100 * float(num1))   (30 % 100 * float(num2))   (30 % 100 * float(school))
    r = r / 100
    print(r)
    print('\nnum1 :', {num1}, '\nnum2 :', {num2}, "\nschool :", {school})

while True:
    main()
    if input("\nWant To continue ? Y/N\n").upper().strip() != "Y":
        break

It's showing me this when I try to type for example my name "trais" or a letter or a "string"

Traceback (most recent call last):
  File "D:\Python Projects\your mark\mark calculator.py", line 51, in <module>
    main()
  File "D:\Python Projects\your mark\mark calculator.py", line 9, in main
    num1 = float(num1)
ValueError: could not convert string to float: 'trais'

CodePudding user response:

You can use PyInputPlus. If you are okay with installing library.

float() will convert numeric string to float type. Not any other string which is not numerical.

Basic snippet for keep on going until input is correct (without any prompt)

def trial():
    try:
        a=float(input("Enter 0 to 100"))
        print(a,type(a))
    except:
        print("Enter number between 0 to 100")
        trial()

CodePudding user response:

guys i really don't know how to thank you, i did it thanks for you all i will show the code for those who want to see it

import time


def main():
    num1 = input("your num1\n")
    num2 = input("your num2\n")
    school = input("your school\n")
    try:
        num1 = float(num1)
        num2 = float(num2)
        school = float(school)
    except:
        print("must be a number")
        time.sleep(1)
    try:
        if num1 > 100:
            print("must be bigger than 0 and less than 100")
            while True:
                time.sleep(1)
                main()
        elif num1 < 0:
            print("must be bigger than 0 and less than 100")
            while True:
                time.sleep(1)
                main()
        if num2 > 100:
            print("must be bigger than 0 and less than 100")
            while True:
                time.sleep(1)
                main()
        elif num2 < 0:
            print("must be bigger than 0 and less than 100")
            while True:
                time.sleep(1)
                main()
        if school > 100:
            print("must be bigger than 0 and less than 100")
            while True:
                time.sleep(1)
                main()
        elif school < 0:
            print("must be bigger than 0 and less than 100")
            while True:
                time.sleep(1)
                main()
        else:
            r = (40 % 100 * float(num1))   (30 % 100 * float(num2))   (30 % 100 * float(school))
        r = r / 100
        print(r)
        print('\nnum1 :', {num1}, '\nnum2 :', {num2}, "\nschool :", {school})
    except:
        while True:
            main()
            time.sleep(1)
            if input("\nWant To continue ? Y/N\n").upper().strip() != "Y":
                break


while True:
    main()
    time.sleep(1)
    if input("\nWant To continue ? Y/N\n").upper().strip() != "Y":
        break

thanks again.

  • Related