簡體   English   中英

如果函數返回用戶輸入的值,如何在不要求用戶輸入另一個值的情況下在另一個函數中使用該值

[英]If a function returns a value entered by a user, how can I use that value in another function without asking the user to input another value

def userChoice():
    print("Press 1 to send text message\n Press 2 to share file")
    action = int(input("Please what do you want to do ? "))
    return action


def useValue():
    value=userChoice() + 1
    print(value)

userChoice()
useValue()

""" 我發現第二個函數再次要求用戶輸入,而我不想要那個,相反我只需要用戶在第一個函數中已經給出的值 """

您需要獲取userChoice()的結果並將其作為參數傳遞給useValue() 嘗試這個:

def userChoice():
    print("Press 1 to send text message\n Press 2 to share file")
    action = int(input("Please what do you want to do ? "))
    return action


def useValue(choice):
    value=choice + 1
    print(value)

user_input = userChoice()
useValue(user_input)

按照您的方式,對userChoice的第一次調用從用戶userChoice獲取輸入,並且從不對其進行任何操作。 然后當useValue被調用時,它會獲取另一個用戶輸入並將其用於打印輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM