我收到一条错误消息,说“raw_input() 需要 1 到 2 个位置参数,但给出了 4 个”

我正在尝试为一个简单的游戏编写代码。游戏的设计很好。但是我很难在原始输入函数中包含一个变量以及文本。任何帮助,将不胜感激。


这是代码


while True:

    n=int(input("How many marbles to start with? ")) #Exercise 2.4

    if (n>=1) and ((n%1)==0):

        break

    else:

        print("enter a positive integer")

while n>0:

    p2=int(input("Player #1, there are",n, "marbles left. How many marbles will you take? "))

    if (p2 not in (1,2,3)) or (p2>n):

        print("not a proper input")

    else:

        n=n-p2

        if n==0:

            print("Player #1, you took the last marble and have won! ")   

        else:

            while True:

                p1=int(input("Player #2, there are",n, "marbles left. How many marbles will you take? "))

                if (p1 not in (1,2,3)) or (p1>n):

                    print("not a proper input")

                else:

                    n=n-p1

                    if n==0:

                        print("Player #2, you took the last marble and have won! ")

                    break

这是我收到的错误消息


TypeError                                 Traceback (most recent call last)

<ipython-input-13-d507a539ca61> in <module>

      6         print("enter a positive integer")

      7 while n>0:

----> 8     p2=int(input("Player #1, there are",(n), "marbles left. How many marbles will you take? "))

      9     if (p2 not in (1,2,3)) or (p2>n):

     10         print("not a proper input")


TypeError: raw_input() takes from 1 to 2 positional arguments but 4 were given


HUH函数
浏览 300回答 1
1回答

哈士奇WWW

您需要将要传递的字符串连接input()成一个字符串,而不是像现在这样将它们作为单独的参数传递。代替:input("Player&nbsp;#1,&nbsp;there&nbsp;are",(n),&nbsp;"marbles&nbsp;left.&nbsp;How&nbsp;many&nbsp;marbles&nbsp;will&nbsp;you&nbsp;take?&nbsp;")和input("Player&nbsp;#1,&nbsp;there&nbsp;are&nbsp;"&nbsp;+&nbsp;str(n)&nbsp;+&nbsp;"&nbsp;marbles&nbsp;left.&nbsp;How&nbsp;many&nbsp;marbles&nbsp;will&nbsp;you&nbsp;take?")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python