2017-03-15 70 views
1
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值吗?, 任何帮助表示。迷宫游戏 - 输入预计最多1个参数,得到3

回答

0

给出input三个参数(字符串,整数,字符串)。下面是用它打印数量的参数的函数演示:

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

你需要的是给一个字符串input

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

不是100%肯定你的意思,我很新的蟒蛇,但我会尽量采用哪些你说,谢谢。 foo和buzz在哪里出现?我很困惑 – MiniMiloe

+0

@BrandonFarmiloe当你调用'input'函数时,你给它三个参数。 “输入”最多只能有一个参数。为了举例,我选择了短字符串,而不是复制你的字符串。 – timgeb

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.") 
+0

我改变了它,但我仍然收到错误消息:| – MiniMiloe

+0

您是否更改了所有打印件? – cdarke

+1

你的意思是输入:) – timgeb

0

所以问题是,你在输入了比对参数的更多功能不像印刷品,你不能这样做。使用加号可以很容易地解决这个问题,以便将字符串连接起来而不是逗号,但要做到这一点,您必须通过将str(屏蔽)转换为int来将其转换为字符串。这是我写的Python 2.7,所以你可能不得不改变一些东西,但它应该都在那里。我还为if语句添加了.upper(),以便它可以同时使用大写和小写输入。 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.")