Why do I get a ValueError: could not convert string to float in Python?
Why do I get a ValueError: could not convert string to float in Python?
我不明白为什么我想转换一个微积分 ValueError string[=49] =] 转换为 float。我想要一个问题,因为我被困住了。
(我的代码的目的是根据问题编号创建级别不断增加的随机方程。)(我仍然是初学者,如果我的代码不规范(而且是法语),我深表歉意。)
:)
有我的代码:
(输入)
from random import *
def Numéro_Question():
global NuméroQuestion
NuméroQuestion+=1
print("\t~ Question {} ~\t\n".format(NuméroQuestion))
def Calcul2():
PremierChiffre=randint(0, NuméroQuestion*5+5)
Question=str(PremierChiffre)
for i in range(1,NuméroQuestion+1):
SigneDeCalcul=["+","*","-"]
SigneChoisi=str(choice(SigneDeCalcul))
x=str(randint(0, NuméroQuestion*5+5))
Question=Question+SigneChoisi+x
print(type(Question))
QuestionNumérique=float(QuestionNumérique)
QuestionEcrite=Question+" = "
Question=float
Réponse=input(QuestionEcrite)
NuméroQuestion=0
Raté=0
while Raté<3:
Numéro_Question()
Calcul2()
print("\n\n")
(输出)
(每次执行程序输出都会改变,因为它给出了随机数)
~ Question 1 ~
<class 'str'>
回溯(最近调用最后):
文件“main.py”,第 26 行,在 <模块>
Calcul2()
文件 "mai,.py", ligne 17, in Calcul2
QuestionNumérique=float(QuestionNumérique)
ValueError:无法将字符串转换为浮点数:'3*6'
这是因为当您使用 float(my_string)
时,它只有在 my_string
可以转换为实际浮点数时才有效。它不能为你做乘法。
幸运的是,有一个非常有用的 python 函数可以接受字符串并将它们作为代码运行。它被称为eval
。
例如,eval("12 + 3")
将 return 15
。
只需使用 eval
而不是 float
,像这样:
QuestionNumérique=eval(QuestionNumérique)
总而言之,您想“评估”(eval
) 数字问题,而不是“投射”(float
) 它。
警告:正如其他人指出的那样,eval
是“不安全的”。通常,将任意字符串作为代码进行评估是不安全的。
更新:我之前吃薯条的时候在想这个,我有一个疯狂的想法。
好的,因此,在 Python 中,您可以在子进程中执行 shell 命令并通过管道传输结果。 (os.popen
- 参见 here) . Assuming the machine running python has Bash as its shell, we can use Bash's arithmetic expression 哪个(我认为)可能更容易防止任意输入。
这是它的样子:
import os
QuestionNumérique = "117 * 3 + 23 * 3"
shell_command = f"echo $(({QuestionNumérique}))"
piped_shell_result = os.popen(shell_command).read()
stripped_result = piped_shell_result.strip()
result_as_number = float(stripped_result)
此代码仍然很危险! 您必须确保 QuestionNumérique
字符串包含“全局关闭”(
和 )
括号,因为有人可以很容易地提供这样的恶意输入:
QuestionNumérique = "117 * 3)); mkdir 'YOU HAVE BEEN H&CK3D'; echo $(("
如果你能确定输入的字符串正确地关闭了括号那么这应该比 eval
更安全,因为 bash 算术表达式只会做算术运算。
我不明白为什么我想转换一个微积分 ValueError string[=49] =] 转换为 float。我想要一个问题,因为我被困住了。
(我的代码的目的是根据问题编号创建级别不断增加的随机方程。)(我仍然是初学者,如果我的代码不规范(而且是法语),我深表歉意。)
:)
有我的代码:
(输入)
from random import *
def Numéro_Question():
global NuméroQuestion
NuméroQuestion+=1
print("\t~ Question {} ~\t\n".format(NuméroQuestion))
def Calcul2():
PremierChiffre=randint(0, NuméroQuestion*5+5)
Question=str(PremierChiffre)
for i in range(1,NuméroQuestion+1):
SigneDeCalcul=["+","*","-"]
SigneChoisi=str(choice(SigneDeCalcul))
x=str(randint(0, NuméroQuestion*5+5))
Question=Question+SigneChoisi+x
print(type(Question))
QuestionNumérique=float(QuestionNumérique)
QuestionEcrite=Question+" = "
Question=float
Réponse=input(QuestionEcrite)
NuméroQuestion=0
Raté=0
while Raté<3:
Numéro_Question()
Calcul2()
print("\n\n")
(输出)
(每次执行程序输出都会改变,因为它给出了随机数)
~ Question 1 ~
<class 'str'>
回溯(最近调用最后):
文件“main.py”,第 26 行,在 <模块>
Calcul2()
文件 "mai,.py", ligne 17, in Calcul2
QuestionNumérique=float(QuestionNumérique)
ValueError:无法将字符串转换为浮点数:'3*6'
这是因为当您使用 float(my_string)
时,它只有在 my_string
可以转换为实际浮点数时才有效。它不能为你做乘法。
幸运的是,有一个非常有用的 python 函数可以接受字符串并将它们作为代码运行。它被称为eval
。
例如,eval("12 + 3")
将 return 15
。
只需使用 eval
而不是 float
,像这样:
QuestionNumérique=eval(QuestionNumérique)
总而言之,您想“评估”(eval
) 数字问题,而不是“投射”(float
) 它。
警告:正如其他人指出的那样,eval
是“不安全的”。通常,将任意字符串作为代码进行评估是不安全的。
更新:我之前吃薯条的时候在想这个,我有一个疯狂的想法。
好的,因此,在 Python 中,您可以在子进程中执行 shell 命令并通过管道传输结果。 (os.popen
- 参见 here) . Assuming the machine running python has Bash as its shell, we can use Bash's arithmetic expression 哪个(我认为)可能更容易防止任意输入。
这是它的样子:
import os
QuestionNumérique = "117 * 3 + 23 * 3"
shell_command = f"echo $(({QuestionNumérique}))"
piped_shell_result = os.popen(shell_command).read()
stripped_result = piped_shell_result.strip()
result_as_number = float(stripped_result)
此代码仍然很危险! 您必须确保 QuestionNumérique
字符串包含“全局关闭”(
和 )
括号,因为有人可以很容易地提供这样的恶意输入:
QuestionNumérique = "117 * 3)); mkdir 'YOU HAVE BEEN H&CK3D'; echo $(("
如果你能确定输入的字符串正确地关闭了括号那么这应该比 eval
更安全,因为 bash 算术表达式只会做算术运算。