迷宫游戏输入最多需要1个参数,得到3个

2024-05-14 19:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

import random
from random import randint


print("Welcome to Brandon's Maze Game",
  "You have to get 10 shields or find the exit to win",
  "Good Luck :D")


counter = 0
shields = 3
fate = randint(1,2)
direction = input("You have come to a stop, do you want to turn Left(L) or      Right(R)? ")

if direction == "L":
    if fate == 1:
        shields += 3
        counter += 1
        direction = input("You came across a chest, you now have ",shields,  "! What way do you want to go next? Left(L) or Right(R)? ")
    if fate == 2:
        shields -= 1
        counter += 1
        direction = input("Uh oh, you got attacked and lost a shield, you now have ",shields," shields. Do you want to go Left(L) or Right(R)? ")


if direction == "R":
    if fate == 1:
        shields += 3
        counter += 1
        direction = input("You came across a chest, you now have ",shields, "! What way do you want to go next? Left(L) or Right(R)? ")
    if fate == 2:
        shields -= 1
        counter += 1
        direction = input("Uh oh, you got attacked and lost a shield, you now have ",shields," shields. Do you want to go Left(L) or Right(R)? ")

if counter == 10:
        print("Congratulations, you made it to the end with ",shields,"  shields.")

我正在做一个迷宫游戏,用户可以选择向左(“L”)或向右(“R”),然后程序会做出选择,是让玩家找到胸部还是被攻击。当用户找到一个聊天,他们得到+3盾牌,如果他们被攻击,他们失去1盾牌。 当我输入“L”或“R”时,它会显示:在第19行 TypeError:输入最多需要1个参数,得到3个。 不确定发生了什么,因为我只输入了1个值?, 感谢您的帮助。你知道吗


Tags: ortorightyoushieldsinputifhave
3条回答

正在给出input三个参数(string,int,string)。下面是一个演示,其中有一个函数可以打印其参数数:

>>> shields = 42
>>> def foo(*args):
...     print(len(args))
... 
>>> foo('fiz', shields, 'buzz')
3

你想要的是给input一个字符串:

>>> foo('fiz {} buzz'.format(shields))
1

input()print()不同。提示是单个字符串,因此将字符串与+(其中有逗号)连接起来。请注意,必须将非字符串(如int)转换为字符串。你知道吗

print("Congratulations, you made it to the end with " + str(shields) + " shields.")

或字符串格式:

print("Congratulations, you made it to the end with {:d} shields.".format(shields))

如果使用的是python3.6,则使用文本字符串插值

print(f"Congratulations, you made it to the end with {shields} shields.")

所以问题是,在输入函数中有超过on的参数,这与print不同,是无法做到的。使用加号而不是逗号将字符串连接在一起可以很容易地解决这个问题,但是要做到这一点,您必须将int-shield转换为字符串,只需放入str(shield)来转换它。这是我编写的Python2.7,所以您可能需要更改一些内容,但应该都在那里。我还将.upper()添加到if语句中,以便它可以接受大小写输入。ps对于Python3,您将执行input(),而不是raw\u input()。对不起,我在2.7中写的我不是3的好。如果你有什么问题尽管问

import random
from random import randint


print("Welcome to Brandon's Maze Game",
  "You have to get 10 shields or find the exit to win",
  "Good Luck :D")


counter = 0
shields = 3
fate = randint(1,2)
direction = raw_input("You have come to a stop, do you want to turn Left(L) or Right(R)? ")

if direction.upper() == "L":
    if fate == 1:
        shields += 3
        counter += 1
        direction = raw_input("You came across a chest, you now have "+str(shields)+  "! What way do you want to go next? Left(L) or Right(R)? ")
    if fate == 2:
        shields -= 1
        counter += 1
        direction = raw_input("Uh oh, you got attacked and lost a shield, you now have "+str(shields)+" shields. Do you want to go Left(L) or Right(R)? ")


if direction.upper() == "R":
    if fate == 1:
        shields += 3
        counter += 1
        direction = raw_input("You came across a chest, you now have "+str(shields)+"! What way do you want to go next? Left(L) or Right(R)? ")
    if fate == 2:
        shields -= 1
        counter += 1
        direction = raw_input("Uh oh, you got attacked and lost a shield, you now have "+str(shields)+" shields. Do you want to go Left(L) or Right(R)? ")

if counter == 10:
        print("Congratulations, you made it to the end with "+str(shields)+"  shields.")

相关问题 更多 >

    热门问题