简体   繁体   English

将列表传递给Python函数并打印

[英]Passing list to function in Python and printing it

I am trying to print the week corresponding to the number that user enters,for eg if user enters 2 return should be Tuesday and so on.But when I run the program it just doesnt ask for user input.Please tell me as to where I am making a mistake. 我正在尝试打印与用户输入的号码相对应的星期,例如,如果用户输入2,返回应该是星期二,依此类推。但是当我运行程序时,它只是不要求用户输入。请告诉我我在哪里在犯一个错误。

def return_day(num):
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    num = input ("Please enter the number: ")
    if int(num) <= 7:
        print(return_day( days[num - 1]))
    else:
        print("None")
 print(return_day( days[num - 1]))

that line was your problem. 那条线是你的问题。 your function is doing too many things at once, separate out the input from the function definition, like so: 您的函数一次执行太多操作,将输入与函数定义分开,如下所示:

def return_day(num):
 days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
 print(days[num])

num = input ("Please enter the number: ")
if int(num) <= 7:
 return_day(int(num)-1)
else:
 print("None")

First, you don't need recursion here. 首先,您不需要在这里递归。 The second, you don't need to give a num variable as a function argument, if you get it from input() function. 第二,如果您是从input()函数获得的,则不需要将num变量用作函数参数。

def return_day():
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    num = input("Please enter the number: ")
    if int(num) <= 7:
        print(days[num - 1])
    else:
        print("None")

The third, you need to call a function after definition: 第三,您需要在定义后调用一个函数:

return_day(4)

Then you will be asked for input 然后将要求您输入

def return_day():
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    num = input("Please enter the number: ")
    if int(num) <= 7:
        print(days[int(num) - 1])
    else:
        print("None")


if __name__ == "__main__":
    return_day()

Explanation: 说明:

As the input from stdin is of string data type it should be converted to integer using int() at line print(days[int(num) - 1]) 由于stdin的输入为string数据类型,因此应在print(days[int(num) - 1])行使用int()将其转换为整数。

I'm not certain why you even need the user input via input() considering that in your example, the user's choice will already be passed directly to the function as an argument. 考虑到在您的示例中,用户的选择将已经直接作为参数传递给函数,因此我不确定您为什么甚至需要通过input()进行用户输入。

def return_day(num):
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    try:
        if int(num) < 8:
            print(days[num-1])
        else:
            print("None")
    except Exception as error:
        print('Caught this error: ' + repr(error))

Output: 输出:

return_day(2)
Tuesday

return_day(8)
None

return_day('asdf')
Caught this error: ValueError("invalid literal for int() with base 10: 'asdf'",)

Also, it is worth mentioning that None (without quotes) is a reserved word in python so would be best to avoid using this for numerous reasons, one of which is that it could lead to future confusion when debugging your application. 另外,值得一提的是,None(不带引号)是python中的保留字,因此出于多种原因最好避免使用它,其中之一是在调试应用程序时可能导致将来的混乱。

Try this code: 试试这个代码:

def return_day():
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    num = int(input ("Please enter the number: "))      #changes here

    if(num <= 7):            #changes here
        print(days[num - 1]) #changes here
    else:
        print("None")

return_day()

Mistakes you were doing. 您正在做的错误。

  1. never calling the function. 永不调用该函数。
  2. using input(num) instead of just num in if condition. 在条件中使用input(num)而不是num。
  3. dont have to use return_day(days in print but just days. 不必使用return_day(打印的天数,但仅几天。

Just take out the taking input outside of method and do the casting at time instead every time: 只需取出方法外部的takeing输入并每次都及时进行转换:

def return_day(num):
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    if num <= 7 :
        return (days[num - 1])
    else:
        return "None"

def main():
    num = int(input ("Please enter the number: "))
    print(return_day(num))

if __name__ == "__main__":
    main()

OUT

Please enter the number: 5
Friday
def return_day(num):
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    print(days[num])    


num = input ("Please enter the number: ")
if int(num) <= 7:
    return_day( num - 1)
else:
    print("None")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM