0

Apologies for the bad title, I wasn't quite sure how to sum up my issue.

I'm working on a "positivity generator" that gives the user positive affirmation based on their mood on a scale of 1-10. Here is the code and my question.

import random

A = "You're looking great today!"
B = "Remember, coding takes practice. You're doing great!"
C = "It's okay to take a break every once in awhile."
D = "You matter."
E = "Mental health is as important as physical health."
F = "Take a nap, you deserve it!"
G = "Good luck today!"

def main():
  print("""Positivity Generator
  ---------------------""")
  greet()
  if int(input("Whats your mood on a scale of 1-10? ")) <= 3:
    print(random.choice([B, D, E]))
  elif input == _ in range(4, 7):
    print(random.choice([C, F]))
  else:
    print(random.choice([A, G]))

def greet():
  name = input("What is your name? ")
  name = name.title()
  print(f"Hello, {name}")
main()

How I would be able to get separate results with 3 different ranges (<=3, from 4 to 7, and >=8) using an if statement.

GTAG
  • 3
  • 3
  • 2
    Assign the value returned by the call to `input()` to a variable, like `mood` and then check the value of `mood` in the expressions in the `if` statement. Also, you'd want something like `mood in range(4, 8)` since `range()` is up-to-but-not-including and you don't want too loop. – Grismar Oct 24 '22 at 04:25
  • The question was closed as a duplicate, but it would appear to me that OP didn't (just) ask about integer values in some range - so I'll leave the answer. – Grismar Oct 24 '22 at 04:35
  • Thanks! I've been sifting through google for awhile to find an answer. now I can get some sleep. – GTAG Oct 24 '22 at 04:43

1 Answers1

1

Assign the value returned by the call to input() to a variable, like mood and then check the value of mood in the expressions in the if statement. Also, you'd want something like mood in range(4, 8) since range() is up-to-but-not-including and you don't want too loop.

So:

import random

A = "You're looking great today!"
B = "Remember, coding takes practice. You're doing great!"
C = "It's okay to take a break every once in awhile."
D = "You matter."
E = "Mental health is as important as physical health."
F = "Take a nap, you deserve it!"
G = "Good luck today!"


def main():
    # added a newline, since typing a literal newline doesn't work
    print("""Positivity Generator\n---------------------""")
    greet()
    # assigning the result of the call to `input()` (and `int()`) to a variable
    mood = int(input("Whats your mood on a scale of 1-10? "))
    # comparing the value of the variable
    if mood <= 3:
        print(random.choice([B, D, E]))
    # reusing the value in `mood`; 8 is not included in the `range()`
    elif mood in range(4, 8):
        print(random.choice([C, F]))
    else:
        print(random.choice([A, G]))


def greet():
    name = input("What is your name? ")
    name = name.title()
    print(f"Hello, {name}")


# not required, but this ensures `main()` is only executed when the script is 
# run directly, not when it is imported.
if __name__ == '__main__':
    main()

Note that I changed the indentation to be 4 spaces instead of 2, which is default for Python - it's best to follow conventions, which makes your code easier to read and maintain, and to combine it with other code.

There's more improvements possible. For one, you don't need all those variables (and the shouldn't be named using capitals, because Python code typically reserves names with capitals for classes).

So:

# lists in dictionary, for various levels of motivation
motivation = {
    0: [
        "Remember, coding takes practice. You're doing great!",
        "You matter.",
        "Mental health is as important as physical health."
    ],
    1: [
        "It's okay to take a break every once in awhile.",
        "Take a nap, you deserve it!"
    ],
    2: [
        "You're looking great today!",
        "Good luck today!"
    ]
}


def main():
    print("""Positivity Generator\n---------------------""")
    greet()
    mood = int(input("Whats your mood on a scale of 1-10? "))
    # get the appropriate level based on mood
    level = 0 if mood <= 3 else 1 if mood <= 7 else 2
    # print a random motivation message for that level
    print(random.choice(motivation[level]))
Grismar
  • 27,561
  • 4
  • 31
  • 54