简体   繁体   English

Python 3“参数过多”吗?

[英]Python 3 “Too many arguments”?

I am writing a python quiz/calculator for school, and I am having a problem with creating random questions for the quiz. 我正在为学校编写python测验/计算器,但在为测验创建随机问题时遇到问题。

At the moment, I have the code as follows: 目前,我的代码如下:

while True:
    if i + n == 10:
        answer = input("If I have",i,"blue marbles and",n,"yellow marbles in a bag, what percentage of blue marbles are there? ")   
    else:
        i = random.randint(1, 10)
        n = random.randint(1, 10)

However python comes up with an error: 但是python出现错误:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
TypeError: input expected at most 1 arguments, got 5

I have tried multiple ways around it, for example: 我尝试了多种解决方法,例如:

while True:
    question = "If I have",i,"blue marbles and",n,"yellow marbles in a bag, what percentage of blue marbles are there? "
    answer = input(question)

However when printed on the Shell, it prints 'If i have,4 [for example],'blue marbles and',6,'yellow marbles in a bag, what percentage of blue marbles are there?'. 但是,在外壳上打印时,它会打印“如果我在袋子中有4个(例如)蓝色大理石和6,'黄色大理石”,那么蓝色大理石的百分比是多少?”。

As you can see, it prints quote marks and commas, but includes the variables i and n correctly. 如您所见,它打印引号和逗号,但正确包含变量in I have looked at the .strip() function but all tutorials are too confusing but I think that is what I need! 我已经看过.strip()函数,但是所有教程都太混乱了,但是我认为这是我所需要的!

Can anyone make this line of code work, where it just prints the question as an input statement, whilst including the variables? 任何人都可以使这一行代码起作用,在其中仅将问题打印为输入语句,同时包括变量吗?

input() takes just one argument, but you are passing in 5: input()仅接受一个参数,但是您要传入5:

input(
    "If I have",
    i,
    "blue marbles and",
    n,
    "yellow marbles in a bag, what percentage of blue marbles are there? ") 

Join those together into a string first: 首先将它们连接成一个字符串:

input("If I have {} blue marbles and {} yellow marbles in a bag, "
      "what percentage of blue marbles are there? ".format(i, n)) 

This uses the str.format() method to insert the values for i and n into a larger string. 这使用str.format()方法in的值插入更大的字符串中。

You may have gotten confused with print() here, which explicitly takes an arbitrary number of arguments and converts each of those to a string before writing them out to stdout, separating each with a space. 您可能在这里与print()混淆了,后者显式地接受任意数量的参数,然后将每个参数转换为字符串,然后再将其写入标准输出,并用空格分隔。 input() does not offer this feature. input()不提供此功能。

I think your problem is that you're treating the input function call as if it was a print call. 我认为您的问题是您将input函数调用视为print调用。 However, the interface is different for input and print . 但是, inputprint的界面不同。

  • input expects exactly one argument input需要一个参数
  • print will accept multiple arguments print将接受多个参数

So, try creating the prompt like this instead: 因此,尝试改成这样的提示:

prompt = "If I have {} blue marbles and {} yellow marbles etc".format(i, n)
input(prompt)

The answer has already been given; 答案已经给出; but I will provide some insight since you seem like a beginner. 但由于您看起来像个初学者,因此我将提供一些见解。 Input() and Print() are built-in functions. Input()和Print()是内置函数。 They are no different than functions you would create yourself. 它们与您将要创建的功能没有什么不同。 Functions are basically a block of code that is run when called. 函数基本上是在调用时运行的代码块。 Functions have arguments, which are values you pass to the function. 函数具有参数,这是您传递给函数的值。 So my add function would go as follows: 因此,我的add函数将如下所示:

def add(x, y)
    answer = x + y
    print(answer)

I would invoke it by typing: 我可以通过键入以下内容来调用它:

add(10, 20)

and the program/interactive shell would return 30. 并且程序/交互式外壳将返回30。

This is why Input() and Print() behave differently, because Print() is meant to take many arguments and concatenate them into one large string, and Input only accepts one. 这就是Input()和Print()行为不同的原因,因为Print()旨在接受许多参数并将它们连接成一个大字符串,而Input只接受一个。 If you ever get stuck, or aren't quite sure how to use a function, module, etc. Python has a built in help() function (that takes one argument, lol). 如果您遇到困难,或者不确定如何使用函数,模块等,则Python具有内置的help()函数(带有一个参数,哈哈)。

So typing help(input) would tell you that input would only take a single argument. 因此,键入help(input)会告诉您输入将只包含一个参数。 This will be very useful if you forget the exact syntax or specifics of something. 如果您忘记某些东西的确切语法或细节,这将非常有用。

Hope this clears up any questions you had, especially if you weren't sure why the given answers were working and yours wasn't. 希望这可以解决您的任何问题,尤其是如果您不确定为什么给出的答案有效而您的答案无效时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM