2017-03-15 37 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.")