【发布时间】:2019-12-29 05:15:48
【问题描述】:
如何定义一个名为 information 的函数来接受用户输入,变量是(name、birth_year、fav_color 和 hometown)。它应该按此顺序返回这些变量的元组。
【问题讨论】:
-
像def信息(*args)一样写:
如何定义一个名为 information 的函数来接受用户输入,变量是(name、birth_year、fav_color 和 hometown)。它应该按此顺序返回这些变量的元组。
【问题讨论】:
这可能就是你要找的东西
def information():
name = input("Insert your name.\n")
birth_year = input("Insert your year of birth.\n")
fav_color = input("Insert your favourite color.\n")
hometown = input("Insert your hometown's name.\n")
return (name, birth_year, fav_color, hometown)
【讨论】:
你可以这样做:
def information():
data = ()
name = input('Enter your name: ')
yob = input('Enter your year of birth: ')
fav_color = input('Enter your favourite colour: ')
hometown = input('Enter your hometown: ')
data = (name, yob, fav_color, hometown)
return data
【讨论】:
您可以在函数中使用input。
def info():
name=input("enter name")
birth_year=input("DOB")
fav_color=input("fav color")
home_town=input("Home town")
return (name,birth_year,fav_color,home_town)
输出:
>>> info()
enter namech3ster
DOB31st feb
fav colorblack
Home townxxxxx
('ch3ster', '31st feb', 'black', 'xxxxx')
>>>
【讨论】:
其实这很简单:
def information():
name = input("what is our name?")
birth_year = input("what year were you born?")
fav_color = input("what is your favourite colour?")
hometown = input("what is your hometown?")
return name, birth_year, fav_color, hometown
您可以稍后通过编写information() 来运行该函数。 input() 函数在函数内部时的行为与往常一样。因此,为此只需将input() 命令放在函数中即可。
【讨论】:
def information():
name = input("Name: ").strip()
# if you want to get birth year as an int, else use it without int.
birth_year = int(input("Birth Year: "))
fav_color = input("Favourite Color: ").strip()
hometown = input("Hometown's name: ").strip()
return (name, birth_year, fav_color, hometown)
【讨论】:
def information(name, birth_year, fav_color, hometown):
return name, birth_year, fav_color, hometown
【讨论】:
def information (name, birth_year, fav_color, hometown):
return (name, birth_year, fav_color, hometown)
info_tups=information("Rupak",1996,"Blue","Dhaka")
print(info_tups)
【讨论】:
def function(name, birth_year, fav_color, hometown):
return (name, birth_year, fav_color, hometown)
这里的返回值是该函数的输入参数的元组。
【讨论】: