在python中如何将用户输入乘以十进制数?

2024-05-15 10:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我在乘法过程中使用字符串和整数有困难。如果有什么事情我不认为我做了什么根本性的错误,但又一次,它不起作用,所以我可能已经做了!在

这是我现在的代码。在

    #This is where I ask the user for input for a value 
UI = print (float(input("Enter your value here: ")))
#Here I have numbers that I need to multiply the input by
FRT = (float(0.290949)
SRM = (float(0.281913)
#Here is the multiplication but this is where the issue occurrs
QV = (float("FRT"*"UI"))
SV = (float("SRM"*"UI"))

最后这两行都有问题,我尝试使用不同的设置来使用数字,而不是将它们定义为FRT和SRM,并在float之前使用integer等等,但是对于这些行,它们都给出了错误“could not convert string to float”FRT


Tags: thetouiforinputhereisvalue
3条回答

你的代码有很多问题

首先,您没有在第4行和第5行关闭括号

其次,您尝试通过字符串调用变量。这不是它的工作原理

QV = (float("FRT"*"UI"))

应该是

^{pr2}$

最后,input已经显示文本,所以您不需要print任何内容,而且由于print返回{},UI将始终是{}。在

UI = print (float(input("Enter your value here: ")))

应该是

UI = float(input("Enter your value here: "))

因此,完整的调试代码应该是:

#This is where I ask the user for input for a value 
UI = float(input("Enter your value here: "))
#Here I have numbers that I need to multiply the input by
FRT = (float(0.290949))
SRM = (float(0.281913))
#Here is the multiplication but this is where the issue occurrs
QV = (float(FRT*UI))
SV = (float(SRM*UI))
    #This is where I ask the user for input for a value 
UI = print (float(input("Enter your value here: ")))

上面不会做你想做的,print不返回任何东西

^{pr2}$

如果使用python2.7us原始输入,则不输入

#Here I have numbers that I need to multiply the input by
FRT = (float(0.290949)
SRM = (float(0.281913)

你只需要:

FRT = 0.290949

无需将浮点数转换为浮点数

#Here is the multiplication but this is where the issue occurrs
QV = (float("FRT"*"UI"))
SV = (float("SRM"*"UI"))

如果要将上面的字符串相乘,请执行以下操作:

QV = FRT * UI

“FRT”是一个字符串,由字母F、R&T组成。如果要引用变量FRT,请不要使用引号。QV = (float("FRT"*"UI"))正在尝试首先将字符串“FRT”乘以字符串“UI”,然后将结果转换为float。由于未定义字符串乘法,因此会出现错误。在

在您当前的代码中,我不确定UI是什么,因为您没有使它等于一个数字,而是一个print()的结果。您的第一行应该替换为

UI = float(input("Enter your value here: "))
print(UI)

然后,QV = FRT * UI将执行您想要的操作,因为FRT和UI都是浮动的。不需要每个操作的括号。在

相关问题 更多 >