ghostface78
ghostface78

Reputation: 51

Connecting input statement to a function in python

I'm a bit new at stack overflow, but I'm trying to program this code so that after the user answers the input question, the code returns an answer outlined in the function hammer_time, how do I prompt the user to input an answer to make the function return what I want it to? Where is my connection between the user input and the function going wrong?

hour = input("What time is it?")

def hammer_time(hour):
    if hour  >= 7 and hour <= 9:
        print("It's breakfast time!")

    elif hour >= 12 and hour <= 2:
        print("It's lunch time!")

    elif hour >= 7 and hour <= 9:
        print("It's dinner time!")

    elif hour >= 10 and hour <= 4:
        print("It's hammer time!")
    else:
        print("Nothing is scheduled at this time.")

Upvotes: 2

Views: 1749

Answers (3)

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

Where is my connection between the user input and the function going wrong?

Well, nowhere actually since you never "connect" them. In this case "connecting" the user input and the function is done by calling the function, passing it the user input as argument, ie:

def hammer_time(hour):
    # your code here

hour = input("What time is it?")
hammer_time(hour)

Note that it's better to have your function returning a value, and doing the print() outside the function, ie:

def hammer_time(hour):
    if hour  >= 7 and hour <= 9:
        return "It's breakfast time!"

    elif hour >= 12 and hour <= 2:
        return "It's lunch time!"

    elif hour >= 7 and hour <= 9:
        return "It's dinner time!"

    elif hour >= 10 and hour <= 4:
        return "It's hammer time!"
    else:
        return "Nothing is scheduled at this time."

hour = input("What time is it?")
msg = hammer_time(hour)
print(msg)

Also, if your using Python3, input() will return a string so all the tests in hammer_time will fail ("3" - string - is not 3 - int), so you may want to make hour an integer:

hour = int(input("What time is it?"))
msg = hammer_time(hour)
print(msg)

If you're using python 2.x, input() will automagically turn the user input into an int if he enters a number, but this is actually a very dangerous feature (the way it's turned into an int is to use eval() which is a huge security hole, I let you google for this), so you want to use raw_input() instead and do the conversion by yourself:

hour = int(raw_input("What time is it?"))

Finally there are a couple logical errors in hammer_time():

    if hour  >= 7 and hour <= 9:
        return "It's breakfast time!"

here:

    elif hour >= 12 and hour <= 2:
        return "It's lunch time!"

a number cannot be greater and equal to 12 AND lesser or equal than 2 at the same time. You will have to use 24h-based notation here (2pm is 12+2=14)

here:

    elif hour >= 7 and hour <= 9:
        return "It's dinner time!"

This case will never happen since it's already been catched by the first if hour >= 7 and hour <= 9 test above. Same problem, you'll need 14h-based notation.

and here:

elif hour >= 10 and hour <= 4:
    return "It's hammer time!"

same problem as above: a number cannot be both >= 10 AND <= 4 at the same time.

Upvotes: 1

Xantium
Xantium

Reputation: 11605

You are never calling your function.

There are several ways you could do this.

One way would be to just call it at the end of your script like this:

hammer_time(int(hour))

Note that variables are not shared in functions so if you define hour inside a function it will not be defined outside unless global is used

Upvotes: 0

Ma0
Ma0

Reputation: 15204

Your mistakes and misunderstandings were highlighted neatly in the comments so I will not repeat them here. Below you will find the corrected code.

def hammer_time(hour):
    if 7 <= hour <= 9:
        print("It's breakfast time!")
    elif 12 <= hour <= 14:
        print("It's lunch time!")
    elif 19 <= hour <= 21:
        print("It's dinner time!")
    elif 22 >= hour or hour <= 4:
        print("It's hammer time!")
    else:
        print("Nothing is scheduled at this time.")


try:
    time = input("What time is it?")
    if ':' in time:
        time = time.split(':')[0]
    hammer_time(int(time))
except ValueError:
    print('Invalid Input!')

Let me know if there is anything you want to know more about.

Note: I would extend hammer time to at least 7 :D

Upvotes: 0

Related Questions