Reputation: 45
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
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
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
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()
Please enter the number: 5
Friday
Upvotes: 0
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.
- never calling the function.
- using input(num) instead of just num in if condition.
- dont have to use return_day(days in print but just days.
Upvotes: 0
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
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
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