Home > Enterprise >  python functions and if statements
python functions and if statements

Time:04-28

user_input_num = int(input("please select a number between 0 - 100 !"))
def jokes(moon,ocean,construction,tooth,circus):
        moon = print(("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."))
        ocean = print(("What did one ocean say to the other ocean? \n Nothing, it just waved."))
        construction = print(("Do you want to hear a construction joke? \n Sorry, I’m still working on it."))
        tooth = print(("What do dentists call their x-rays? \n Tooth pics!"))
        circus = print(("Did you hear about the fire at the circus? \n It was in tents!"))

        if (user_input_num > 0 and  user_input_num < 21):
            jokes(moon)

        elif (user_input_num > 21 and  user_input_num < 41):
            jokes(ocean)

        elif (user_input_num > 41 and  user_input_num < 61):
            jokes(construction)

        elif (user_input_num > 61 and  user_input_num < 81):
            jokes(tooth)

        elif (user_input_num > 81 and  user_input_num < 101):
            jokes(circus)

        else: print("Error number not within specified range")

user should be able to input number between 1-100 and a joke will be returned , joke does not print help please

CodePudding user response:

need to be like this

def jokes(user_input_num):
        moon = "Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."
        ocean = "What did one ocean say to the other ocean? \n Nothing, it just waved.")
        construction = "Do you want to hear a construction joke? \n Sorry, I’m still working on it."

        if (user_input_num > 0 and  user_input_num < 21):
            print(moon)
    ....

then call your jokes method like

user_input_num = int(input("please select a number between 0 - 100 !"))
jokes(user_input_num)

Side note:

  1. You can also use if 0 < user_input_num < 21 in your condition for better readability
  2. I was trying to think of a more elegant approach by using dict but looks like if-else is the best way dealing with ranges, something to read before going to sleep https://stackoverflow.com/a/45075450/342553

CodePudding user response:

your indents is misplaced...just remove them, and use print directly

user_input_num = int(input("please select a number between 0 - 100 !"))       

if (user_input_num > 0 and  user_input_num < 21):
    print("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere.")
elif (user_input_num > 21 and  user_input_num < 41):
    print("What did one ocean say to the other ocean? \n Nothing, it just waved.")
elif (user_input_num > 41 and  user_input_num < 61):
    print("Do you want to hear a construction joke? \n Sorry, I’m still working on it.")
elif (user_input_num > 61 and  user_input_num < 81):
    print("What do dentists call their x-rays? \n Tooth pics!")
elif (user_input_num > 81 and  user_input_num < 101):
    print("Did you hear about the fire at the circus? \n It was in tents!")   
else: print("Error number not within specified range")

CodePudding user response:

You have to call the function to make it execute the code inside it. I suggest modifying the parameters to be user_input_num so it makes more sense and runnable.

user_input_num = int(input("please select a number between 0 - 100 !"))
def jokes(user_input_num):
        moon = print(("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."))
        ocean = print(("What did one ocean say to the other ocean? \n Nothing, it just waved."))
        construction = print(("Do you want to hear a construction joke? \n Sorry, I’m still working on it."))
        tooth = print(("What do dentists call their x-rays? \n Tooth pics!"))
        circus = print(("Did you hear about the fire at the circus? \n It was in tents!"))

        if (user_input_num > 0 and  user_input_num < 21):
            jokes(moon)

        elif (user_input_num > 21 and  user_input_num < 41):
            jokes(ocean)

        elif (user_input_num > 41 and  user_input_num < 61):
            jokes(construction)

        elif (user_input_num > 61 and  user_input_num < 81):
            jokes(tooth)

        elif (user_input_num > 81 and  user_input_num < 101):
            jokes(circus)

        else: print("Error number not within specified range")
jokes(user_input_num)

CodePudding user response:

The simple way, where you don't need to use variables (e.g. moon, ocean) to reference what to print.

def jokes(user_input_num):
    if (0 < user_input_num  < 21):
        print(("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."))
    elif (21 < user_input_num < 41):
        print(("What did one ocean say to the other ocean? \n Nothing, it just waved."))
    elif (41 < user_input_num < 61):
        print(("Do you want to hear a construction joke? \n Sorry, I’m still working on it."))
    elif (61 < user_input_num < 81):
        print(("What do dentists call their x-rays? \n Tooth pics!"))
    elif (81 < user_input_num < 101):
        print(("Did you hear about the fire at the circus? \n It was in tents!"))
    else: print("Error number not within specified range")

user_input_num = jokes(int(input("please select a number between 0 - 100 !")))

or the dictionary way, which is similar to what you are doing, just make sure the keywords (e.g. moon, ocean) is a key in dictionary, not a variable.

def jokes(keyword):
    joke = {
        "moon":"Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere.",
        "ocean":"What did one ocean say to the other ocean? \n Nothing, it just waved.",
        "construction":"Do you want to hear a construction joke? \n Sorry, I’m still working on it.",
        "tooth":"What do dentists call their x-rays? \n Tooth pics!",
        "circus":"Did you hear about the fire at the circus? \n It was in tents!"
    }
    print(joke[keyword])

user_input_num = int(input("please select a number between 0 - 100 !"))

if (user_input_num > 0 and  user_input_num < 21):
    jokes("moon")
elif (user_input_num > 21 and  user_input_num < 41):
    jokes("ocean")
elif (user_input_num > 41 and  user_input_num < 61):
    jokes("construction")
elif (user_input_num > 61 and  user_input_num < 81):
    jokes("tooth")
elif (user_input_num > 81 and  user_input_num < 101):
    jokes("circus")
else: print("Error number not within specified range")

Btw, nice jokes.

  • Related