ValueError:无法将字符串转换为float:在Python 3.10中



当有人写入字符串或字母时,我希望代码使他们返回,并且打印"的代码必须是大于0且小于100"的数字,但实际发生的是代码关闭。有办法解决这个问题吗?

import time
def main():
num1 = input("your t76elyn")
num2 = input("your num2n")
school = input("your schooln")
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/Nn").upper().strip() != "Y":
break

当我试着输入例如my name "trais"或字母或"字符串">

Traceback (most recent call last):
File "D:Python Projectsyour markmark calculator.py", line 51, in <module>
main()
File "D:Python Projectsyour markmark 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 num1n")
num2 = input("your num2n")
school = input("your schooln")
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/Nn").upper().strip() != "Y":
break

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

这里发生的事情是,如果你不小心输入了一个字符串,代码不会中断,但你会看到这样的错误消息,你可以越来越多地使用这些代码!

i used here "try"one_answers";except"方法这里有一个教程!https://www.w3schools.com/python/python_try_except.asp: ~:文本= % 20试% 20块% 20让你% 20,当% 20 % 20 % 20没有% 20误差

非常感谢@Noah

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

正如你所看到的,伙计们,我输入了我的名字,代码告诉我它必须是一个数字,你可以继续代码而不退出或打破"),再次感谢

最新更新