【问题标题】:Define a function called information that takes user input定义一个名为 information 的函数,该函数接受用户输入
【发布时间】:2019-12-29 05:15:48
【问题描述】:

如何定义一个名为 information 的函数来接受用户输入,变量是(name、birth_year、fav_color 和 hometown)。它应该按此顺序返回这些变量的元组。

【问题讨论】:

  • 像def信息(*args)一样写:

标签: python function input


【解决方案1】:

这可能就是你要找的东西

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)

【讨论】:

  • 如果您解释了您提供的代码如何回答问题,这将是一个更好的答案。
  • @pppery。在这种情况下,我会说这是不言自明的
【解决方案2】:

你可以这样做:

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

【讨论】:

  • 如果您解释了您提供的代码如何回答问题,这将是一个更好的答案。
【解决方案3】:

您可以在函数中使用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')
>>> 

【讨论】:

    【解决方案4】:

    其实这很简单:

    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() 命令放在函数中即可。

    【讨论】:

      【解决方案5】:
      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)
      

      【讨论】:

      • 如果您解释了您提供的代码如何回答问题,这将是一个更好的答案。
      【解决方案6】:
      def information(name, birth_year, fav_color, hometown):
          return name, birth_year, fav_color, hometown
      

      【讨论】:

      • 如果您解释了您提供的代码如何回答问题,这将是一个更好的答案。
      【解决方案7】:
       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)
      

      【讨论】:

      • 请避免仅使用代码的答案,并简要说明您的代码为何有效。
      【解决方案8】:
      def function(name, birth_year, fav_color, hometown):
          return (name, birth_year, fav_color, hometown)
      

      这里的返回值是该函数的输入参数的元组。

      【讨论】:

      • 您提供的函数没有回答原始问题 - 它没有用户输入,而且使用用户输入的函数确实没有理由有任何参数。
      猜你喜欢
      • 2018-07-04
      • 2021-12-30
      • 1970-01-01
      • 2014-05-19
      • 2020-04-12
      • 2020-09-17
      • 1970-01-01
      • 2020-06-25
      • 2022-10-23
      相关资源
      最近更新 更多