传递列表以在Python中运行并打印它

时间:2018-04-20 03:53:08

标签: python python-3.x

我正在尝试打印与用户输入的数字相对应的一周,例如,如果用户输入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")

7 个答案:

答案 0 :(得分:2)

 print(return_day( days[num - 1]))

那条线是你的问题。你的函数一次做了太多的事情,把输入从函数定义中分离出来,如下所示:

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")

答案 1 :(得分:0)

首先,这里不需要递归。第二,如果你从input()函数得到它,你不需要给一个num变量作为函数参数。

APP_INITIALIZER

第三,你需要在定义后调用一个函数:

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")

然后你会被要求输入

答案 2 :(得分:0)

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()

说明:

由于stdin的输入属于string数据类型,因此应使用行int()处的print(days[int(num) - 1])将其转换为整数

答案 3 :(得分:0)

我不确定为什么你甚至需要通过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))

输出:

return_day(2)
Tuesday

return_day(8)
None

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

另外,值得一提的是,None(没有引号)是python中的保留字,因此最好避免使用它有很多原因,其中之一是它可能会在调试应用程序时导致将来混淆。 / p>

答案 4 :(得分:0)

试试这段代码:

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()

你正在做的错误。

  
      
  1. 从不调用该功能。
  2.   
  3. 在条件中使用输入(num)而不是num。
  4.   
  5. 不必使用return_day(印刷天数,但只需几天。
  6.   

答案 5 :(得分:0)

只需取出方法之外的输入输入,然后每次都进行转换:

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

答案 6 :(得分:0)

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")