【问题标题】:Python - TypeError: can't multiply sequence by non-int of type 'str'Python - TypeError:不能将序列乘以“str”类型的非整数
【发布时间】:2019-04-12 20:07:43
【问题描述】:

这是我的一个学校项目的代码(我写在Python 2.7.13):

def euconverter(mon, rate):
    return (mon * rate)

cur = raw_input('Please give me the currency')
mon = raw_input('Please give me the value')
rate = raw_input('Please give me the rate')
while cur == 'EUR' or cur == 'Eur' or cur == 'GBP' or cur == 'Gbp':
    if cur == 'Eur' or cur == 'Eur':
            print (euconverter(mon, rate))
    elif cur == 'GBP' or cur == 'Gbp':
            print (euconverter(mon, rate))
    else:
        if cur != 'EUR' or cur != 'Eur' or cur != 'GBP' or cur != 'Gbp':
            print 'Wrong input'
            break

我收到此错误:

Traceback (most recent call last):
  File "C:/Users/Maple/PycharmProjects/untitled/Mid term Project.py", line 15, in <module>
    print (euconverter(mon, rate))
  File "C:/Users/Maple/PycharmProjects/untitled/Mid term Project.py", line 2, in euconverter
    return int(mon * rate)
TypeError: can't multiply sequence by non-int of type 'str'

另外,如果我在询问货币类型时输入了一个数值,那么程序会退出而不显示任何消息。 这是一个学校项目,所以我希望从用户那里得到错误的输入,并且需要向他们提供所需的错误消息,同时试图让他们返回并输入正确的错误消息。

【问题讨论】:

  • monrate 是字符串。您需要将它们转换为数字。
  • 您的while 也是一个无限循环,因为您没有在其中读取cur 的新值。
  • @khelwood 部分答案是相同的,是的,但问题还询问有关捕获输入错误的问题
  • @ScottAnderson 是的,但我也不能投票给“太宽泛”。

标签: python python-2.7


【解决方案1】:

如 cmets 中所述,monraw 隐含为 strings。 要转换它们,请使用例如mon = int(mon)mon = float(mon)

请小心,因为您应该注意无效输入(尝试 - 除了TypeError 块)。

小贴士: - 最后如果应该使用and 而不是or - 这两行没有区别:

if cur == 'Eur' or cur == 'Eur':
    print (euconverter(mon, rate))
elif cur == 'GBP' or cur == 'Gbp':
    print (euconverter(mon, rate))

如果这是故意的,您可以将它们组合在一起

【讨论】:

    【解决方案2】:

    看来您正在尝试做的是提示用户输入并验证它,然后再次询问它是否无效。 您可以通过将每个 raw_input() 包装在带有标志的 while-loop 中来尝试。

    为了验证货币,将输入转换为大写,然后再次检查允许的可能性列表会更容易。

    要验证金额和费率,您可以在 try-except 块内将它们转换为 float

    valid_currencies = ['EUR', 'GBP']
    cur = None
    mon = None
    rate = None
    
    is_valid = False
    while not is_valid:
        cur = raw_input('Please give me the currency').upper()
        if cur in valid_currencies:
            is_valid = True
        else:
            print 'Not a valid currency'
    
    is_valid = False
    while not is_valid:
        try:
            mon = float(raw_input('Please give me the value'))
            is_valid = True
        except ValueError:
            print 'Not a valid value'
    
    is_valid = False
    while not is_valid:
        try:
            rate = float(raw_input('Please give me the rate'))
            is_valid = True
        except ValueError:
            print 'Not a valid rate'
    
    print 'Converted amount'
    print mon * rate
    

    【讨论】:

      【解决方案3】:

      您的文本输入的一些重构代码如下所示:

      legal_input = ["gbp", "eur", "yen"]
      currency = raw_input("Please enter the currency type")
      if currency.lower() in legal_input:
          #some code to do the required operations
      else:
          print('not a recognised currency type!')
      

      【讨论】:

      • 我们没有被教导如何使用合法输入,因此我无法将其添加到项目中:/
      • 好的,请阅读我关于为什么您的 while 循环会阻止您捕获任何输入错误的问题的评论
      【解决方案4】:
      def euconverter(mon, rate):
          return mon * rate
      
      
      cur = raw_input('Please give me the currency type ')
      mon = float(raw_input('Please give me the ammount of money '))
      rate = float(raw_input('Please give me the rate of the exchange '))
      while True:
          #i dont know how to make the false statement ending the program as well not necessary i think
          if cur == 'EUR' or cur == 'Eur' or cur == 'GBP' or cur == 'Gbp':
              if cur == 'EUR' or cur == 'Eur':
                  print (euconverter(mon, rate))
                  break
              elif cur == 'GBP' or cur == 'Gbp':
                  print (euconverter(mon, rate))
                  break
                  # I dont know if multiple breaks should be included here
          else:
              print 'wrong input'
              cur = raw_input('Please give me the currency type again correcntly this time ')
              continue
      

      在每个人的提示和帮助下,我终于让它运行起来,提示用户在错误时在货币输入行中输入其他货币感谢您的帮助,您很快就会再次收到我的来信xD

      【讨论】:

        猜你喜欢
        • 2010-11-15
        • 1970-01-01
        • 2020-12-17
        • 1970-01-01
        • 2021-08-07
        • 2017-12-01
        • 2013-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多