Utkarsh gaur
Utkarsh gaur

Reputation: 45

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.

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

Upvotes: 0

Views: 77

Answers (7)

Rafiqul Islam
Rafiqul Islam

Reputation: 1646

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

Upvotes: 0

Kulten
Kulten

Reputation: 119

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

Upvotes: 2

Roushan
Roushan

Reputation: 4420

Just take out the taking input outside of method and do the casting at time instead every time:

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

Upvotes: 0

Hamza Mehboob
Hamza Mehboob

Reputation: 142

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.
  3. dont have to use return_day(days in print but just days.

Upvotes: 0

user9074332
user9074332

Reputation: 2636

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.

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.

Upvotes: 0

user5777975
user5777975

Reputation:

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

Upvotes: 0

Mikhail Sidorov
Mikhail Sidorov

Reputation: 1434

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.

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

Upvotes: 0

Related Questions