迷宫游戏 - 输入预计最多1个参数,得到3

时间:2017-03-15 17:07:11

标签: 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个值?  任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

给予input三个参数(字符串,整数,字符串)。这是一个带有打印其参数数量的函数的演示:

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

您想要的是将一个字符串提供给input

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

答案 1 :(得分:0)

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))

或使用文字字符串插值,如果您使用的是Python 3.6

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

答案 2 :(得分:0)

所以问题是你在输入函数中有多个on参数,这与你不能做的打印不同。这可以使用加号轻松地将字符串连接在一起而不是逗号来解决,但要做到这一点,你必须通过将str(shield)转换为它来将int shield转换为字符串。这就是我编写的python 2.7所以你可能需要改变一些东西,但它应该都存在。我还将.upper()添加到if语句中,因此它可以同时使用大写和小写输入。 ps for python 3你会输入input()而不是raw_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.")