簡體   English   中英

ValueError:無法將字符串轉換為浮點數:在 Python 3.10 中

[英]ValueError: could not convert string to float: in Python 3.10

當有人寫一個字符串或一個字母時,我希望代碼讓他們返回,並且打印“必須是大於 0 且小於 100 的數字”的代碼,但實際發生的是代碼關閉。 有沒有辦法解決這個問題?

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

當我嘗試輸入例如我的名字“trais”或字母或“字符串”時,它向我展示了這一點

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'

您可以使用PyInputPlus 如果您可以安裝庫。

float()將數字字符串轉換為浮點類型。 不是任何其他非數字的字符串。

繼續進行的基本片段,直到輸入正確(沒有任何提示)

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

伙計們,我真的不知道如何感謝你們,我做到了,謝謝大家,我會為那些想看的人展示代碼

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

這里發生的情況是,如果您不小心輸入了一個字符串,代碼不會中斷,但您會看到這樣的錯誤消息,並且您可以越來越多地使用該代碼!

我在這里使用了“嘗試”和“除外”方法,這是一個教程! https://www.w3schools.com/python/python_try_except.asp#:~:text=%20try%20block%20lets%20you,when%20there%20is%20no%20error

非常感謝@Noah

your num1
trais
your num2
34
your school
45
must be a number
your num1

正如你們看到的,我輸入了我的名字,代碼告訴我它必須是一個數字,你可以繼續代碼而不退出或破壞“),再次感謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM