【问题标题】:How to multiply user input by a decimal number in python?如何在python中将用户输入乘以十进制数?
【发布时间】:2016-10-01 21:13:19
【问题描述】:

您好,我在乘法过程中使用字符串和整数时遇到问题。如果有什么我认为我从根本上没有做错任何事情,但话又说回来,它不起作用,所以我可能已经做了!

这是我现在的代码。

    #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 并在浮点数之前使用整数等,但是这些行都给出了错误“可能不将字符串转换为浮点“FRT””

【问题讨论】:

  • 部分括号没有闭合。
  • 你应该使用QV = (float(FRT*UI)) 而不是QV = (float("FRT"*"UI")),因为FRTUI 是变量,但在" " 内部它们被视为字符串......你也不能打印并同时赋值!!!最后你有额外的(
  • 嘿,所以我改变了结尾部分,这个问题现在已经消失了。关于评论的第二部分,这是否意味着在 UI = print (float(input(".......:"))) 行中我试图使输入的值变为 float ?这是否意味着之后,我应该使用 UI 值并使其浮动在单独的行上?对于这些非常糟糕的问题,我很抱歉,我对此很陌生,英语不是我的母语,所以我不能很好地表达句子

标签: python math input floating-point integer


【解决方案1】:

你的代码有很多问题

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

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

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

应该是

QV = (float(FRI * UI))

最后,input 已经显示了文本,所以你不需要print 任何东西,并且由于print 返回None,UI 将始终为None

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))

【讨论】:

  • 另请注意,您使用了不必要的括号,但这是另一天的问题
  • 嗨,很抱歉,当我将原始代码放入此评论时,我没有正确复制它。我没有括号。感谢您非常快速和简洁的帮助。我不知道输入会自行打印,也不知道 " " 将值视为字符串。谢谢。
  • @Gramaru 很高兴能帮上忙!如果您不介意,并且觉得这样做很合适,请将我的答案标记为已接受的答案,这样任何发现此问题的人都可以找到他们需要的答案:)
  • @Gramaru 如果此答案为addressed your problem,请考虑accepting it,方法是单击答案左侧的复选标记/勾选,将其变为绿色。这标志着问题的解决令您满意,并向您和回答者奖励reputation。一旦您拥有 >= 15 的声望点,您也可以根据需要对答案进行投票。也没有义务这样做。
【解决方案2】:

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

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

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

然后,QV = FRT * UI 会做你想做的事,因为 FRT 和 UI 都是浮点数。每个操作都不需要括号。

【讨论】:

    【解决方案3】:
        #This is where I ask the user for input for a value 
    UI = print (float(input("Enter your value here: ")))
    

    上面不会做你想要的,打印不会返回任何东西

    UI = float(input("..."))
    print(UI)
    

    如果使用python 2.7我们raw_input,不输入

    #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
    

    【讨论】:

      【解决方案4】:

      你希望你的输出是什么?

      您犯的错误是这两个字符串是变量,因此请从变量中删除引号,以便您的代码如下所示:

      #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)
      #Remove the quotes from the variables below and you get this
      QV = (float(FRT*UI))
      SV = (float(SRM*UI))
      

      【讨论】:

      • 这里有一些问题:UI 现在是None 并且缺少的括号太多太多。请在发布之前测试您的代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多