将用户输入作为参数传递(Python)

时间:2017-09-17 17:41:43

标签: python

我正在尝试为我的二十一点游戏创建基本问题,我想将用户的输入作为参数传递给我创建的函数。我尝试在终端内运行我的代码,没有任何反应。有人能告诉我我做错了吗?

#function that passes one parameter that prints a question

def ask_user(a):
    print('Would you like to HIT or STAND?')

#variable that holds the user's input

user_answer = input()

#calling the function and passing the users input as the parameter

ask_user(user_answer)

4 个答案:

答案 0 :(得分:1)

终端正在等待您的输入。这将说清楚:

#function that passes one parameter that prints a question
def ask_user(a):
     print('Would you like to HIT or STAND?')

#variable that holds the users input
user_answer = input("type user input here: ")

#calling the function and passing the users input as the parameter
ask_user(user_answer)

根据您的评论,请考虑以下事项:

def ask_user():
    user_answer = input("Would you like to HIT or STAND? ")
    print("the users reply is stored in user_answer. And is",user_answer )
ask_user()

答案 1 :(得分:0)

你的意思是你只是想将一个变量传递给函数并打印它以及另一个打印语句?

def ask_user(a):
    print('Would you like to HIT or STAND?')
    print(a) #if you just want it to print your variable 'a'
    return a   # if you want your function to return your input

def ask_user(a):
    print('Would you like to HIT or STAND?')
    return a   # if you want your function to return your input

def ask_user(a):
    print('Would you like to HIT or STAND?: ', a) # prints both on 1 line

答案 2 :(得分:0)

我建议你做这样的事情:

def ask_user():
    print("Would you like to HIT or STAND?")
    return (input("type use input here: "))

my_var=ask_user()

print ("You decided to", my_var)

答案 3 :(得分:0)

我的方法是这样的,希望这有帮助

def ask_user(a):
        print('Would you like to HIT or STAND?')
        user_answer = input()
        if(user_answer=="HIT"):
              #some code or function call with parameter "HIT"
        elif(user_answer=="STAND"):
              #some code or function call with parameter "STAND"