有没有什么方法可以将用户的输入放在一个变量中,然后在另一个变量中使用这个输入?

2024-06-02 07:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有什么方法可以将用户的输入放在一个变量中,然后在另一个变量中使用这个输入?你知道吗

例如:

def user_input():
    numb = input("Please input the number ")
    numb_process()

def numb_process():
    if numb == '1':
        print("Good")
    else:
        print("Bad")

user_input()

Tags: the方法用户numberinputifdefprocess
2条回答

这是python101,标准python tutorial是学习基本概念的好地方。在您的例子中,简单的参数传递就可以了。你知道吗

def user_input():
    numb = input("Input number ") # bind result of the input function
                                  # to local variable "numb"

    numb_process(numb)            # pass the object bound to "numb"
                                  # to called function

def numb_process(some_number):    # bind inbound object to local variable
                                  # "some_number"

    if some_number == '1':        # use bound object in calculations
        print("Good")
    else:
        print("Bad")

user_input()

应该有用。您可以通过给它一个参数“numb”来调用numb\u process函数,该参数将从函数“user\u input”移动到函数“numb\u process”。你知道吗

def user_input():
    numb = input("Please input the number ")
    numb_process(numb)

def numb_process(numb):
    if numb == '1':
        print("Good")
    else:
        print("Bad")

user_input()

相关问题 更多 >