【问题标题】:difference between " , " and " + " in pythonpython中“,”和“+”的区别
【发布时间】:2016-05-25 19:22:12
【问题描述】:

我正在编写的一些代码有问题。我不断收到错误: 最多输入 1 个参数,得到 3 个

我在http://stackoverflow.com/questions/9969844/error-input-expected-at-most-1-argument-got-3 上找到了它的原因。我将参考 stackoverflow 链接问题中的代码,因为它更易于阅读。

def input_scores():
scores = []
y = 1
for num in range(5):
    score = int(input('Please enter your score for test', y,': '))
    while score < 0 or score > 100:
        print ('Error --- all test scores must be between 0 and 100 points')
        score = int(input('Please try again: '))
    scores.append(score)
    y += 1
     return scores

如果你更换 score = int(input('Please enter your score for test', y,': '))

score = int(input('Please enter your score for test ' + str(y) + ': '))

它解决了问题。

我的问题是你为什么不能使用 , 而不是 +。我曾尝试在网上查找,但找不到此特定问题的答案

【问题讨论】:

    标签: python-3.5


    【解决方案1】:

    当您使用逗号时,您说的是运行带有 3 个参数的 input 函数:'Please enter your score...'y': ',但因为 input 只需要一个参数,所以函数会输出一个错误说明当你给它 3 时期待 1 个参数。

    当您使用加号时,您将运行 input 与 1 个参数字符串 'Please enter your score...' + str(y) + ': '。加号将所有字符串一起添加到一个字符串中,然后输入到输入中。

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 2011-10-25
      • 2019-03-26
      • 2011-09-23
      • 2013-01-17
      • 2011-07-12
      • 2016-01-23
      相关资源
      最近更新 更多