将值传递给游戏结束消息

时间:2015-10-03 14:53:38

标签: python parameters parameter-passing

我有这个挂起的代码,但我想添加参数传递给它

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces:new string[]
            {
                "OldNamespace"
            }
        );
    }
}

我希望它取名字值,用户在开始时输入,然后将该值传递给最终消息。

实施例

import time

import random


#Procedure
def Username():
    name = raw_input("What is your name?")
    print "Hello, "+ name, "Time to play hangman!"
    print "\n"

def loading():
    #Makes the user wait for 1 second
    time.sleep(1)
    print "Start guessing..."
    time.sleep(0.5)
    return

def randwords():
    global words
    words = ["keyboard", "motherboard", "python", "powersupply"]
    words = random.choice(words)

#Main Program
Username()

loading()


randwords()

guesses =""

turns = 12


#while loop


while turns > 0:

    failed = 0

    for char in words:

        if char in guesses: 

            print char,

        else:

            print"_",

            failed += 1    

    if failed == 0:
        print "\nyou won Well Done"


        break
    print


    guess = raw_input("guess a character:")


    guesses += guess

    if guess not in words:


        turns -= 1


        print "wrong\n"


        print "you have", + turns, "more guesses"

        if turns == 0:

            print "you lose GAME OVER\n"


input()
Username()
waiting()

Random

print "you have won (NAME) Well Done"

请帮助我,因为我每次尝试收到错误信息都可以解决问题。

2 个答案:

答案 0 :(得分:0)

您应该声明' name'作为全球变量。

name = '' # before def Username

在为此变量指定值之前,请执行以下操作:

global name
name = raw_input("What is your name?")

然后,您可以在脚本中的任何位置使用此变量来打印其值。

print name, "you lose GAME OVER\n"

P.S:要写入全局变量,必须在:

之前
global <variable-name>

然而,您可以正常方式阅读其值

答案 1 :(得分:0)

您只需从“Username()”函数中返回名称。

def Username():
    ...
    return name

def main():
    ...
    name = Username()
    ...