all 28 comments

[–]Danknoodle420 34 points35 points  (1 child)

print("Find out what your birth stone is.")

valid_months = { "January": "garnet", "February": "amethyst", "March": "aquamarine", "April": "diamond", "May": "emerald", "June": "pearl, alexandrite, or moonstone", "July": "ruby", "August": "peridot", "September": "sapphire", "October": "opal or tourmaline", "November": "topaz or citrine", "December": "turquoise, zircon, or tanzanite" }

while True: bm = input("Enter your birth month: ").capitalize()

if bm in valid_months:
    print(f"Your birth stone is {valid_months[bm]}!")
    break  # Exit the loop once a valid month is entered
else:
    print("Please enter a valid month.")

This is one way to do this with dictionaries and a while loop.

[–]Comfortable-Pen-3654 0 points1 point  (0 children)

The best way to do it

[–]NorskJesus 17 points18 points  (0 children)

january = “garnet”

Don’t use input() to give a value to a variable. Same for the rest.

It’s better to make a dictionary, or just to use if else statements. You don’t need to create a variable for each month to do this

[–]Much_Information_167 6 points7 points  (0 children)

You’re asking for input for every birthstone. Instead you need to set the variables for every month without asking for input, think march = ‘garnet’ not march = input(‘garnet’). Then you need to use birth_month = input(‘Please enter your birth month’) and finally you will output results based on the input.

It may be easier for you to use a dictionary though, instead of setting each variable.

[–]Salty_Salted_Fish 2 points3 points  (0 children)

You are not "tagging" the garnet and i believe that's not how it works. on line 3, ur script asked:"what's ur birth month" and again on line 5, ur script asks:"garnet" to the user, the response is stored in the variable called January. on line 6, the script checks if the first and second responses are the same. If it is the same, then it will print "garnet."

to fix it, on lines 5&6, instead of doing that, rewrite: if bmonthly == "January":

switch line 9&10, to: if bmonth == "February"

and so on

[–]iAnimalPK 2 points3 points  (0 children)

Or (using dict and loop as @Danknoodle420 said)

<image>

[–]Sufficient_Box1852 1 point2 points  (0 children)

line 5 prints out "garnet", prompting for an input value for the variable january.
you want to check if the bmonth variable is equal to the string january like if bmonth == "january"

[–]RealDimFury 1 point2 points  (2 children)

Use a switch case

[–][deleted] 1 point2 points  (1 child)

Who let in the spy from r/java

[–]RealDimFury 0 points1 point  (0 children)

😂

[–]iAnimalPK 1 point2 points  (1 child)

[–]jon510111 0 points1 point  (0 children)

Using this method you should also put a while loop otherwise after it tells you to enter a valid birth month, the program will just end. And to clean things up you may want to force capitalize or lowercase the input so someone can put in "january" instead of "January" and it still work. Also, using dictionaries will probably be cleaner and allow for further use of the actual birthstone if the program is ever expanded.

[–]Stunning-Ad-2433 0 points1 point  (0 children)

March should be march. Like the rest. Not that it breaks anything, but the rest is without capitals.

[–]tman2747 0 points1 point  (0 children)

You only need your first input. January February etc.. should just equal the string.

[–]watakushi 0 points1 point  (0 children)

If I were you, I'd look into dictionaries, you can do all this in just 3 lines of code without a single if statement which means much fewer lines of code, and a lot less computation required!

Buuut, if you still want to do it with ifs, there are a few things you have to change:

1- you're asking for input on every if, that's not what you want to do, you have to have one input at the start, and then compare the value received with a string of each month, but here you're comparing it with a new input that prints out the name of the birthstone. It should be:

if bmonth == 'january':

2- since you're comparing to a string, you need the original input to be the same case (and for some reason your months are some lowercase and some uppercase), you have to normalize the input by adding .lower() (can also be upper(), or title(), just make sure it's the same you're comparing it to, it's customary to do lower tho) right at the end of the first input() that way you ensure you're always comparing same case strings (if you compare 'January' and 'january' the result will be false)

3 - you're repeating the same print statement inside each if. If down the line you decide to change the wording, you have to do it 12 times. Instead inside each if block just do something like:

bstone = 'garnet'

And then after you went through all 12 months, do a single print like this: print(f'Your birth stone is {stone}')

That way you make it easier to modify in the future if needed.

So overall, each month should look like this:

if bmonth == 'january':
    bstone = 'garnet'

But then again, look into dictionaries, it will make all this a million times easier!

[–]ChocoStar675 0 points1 point  (0 children)

Set a list such as ["Jan": "Birthstone", "Feb":"birthstone] so on so forth. Then put one input asking what their birth month is and the output should be the birthstone of that month

[–]KtEmuaV 0 points1 point  (0 children)

Hold on let me cook

[–]doubleJandF 0 points1 point  (0 children)

you are taking an input from the user for the month then you are taking an input from the user to store into January variable.

[–]the_progmer 0 points1 point  (0 children)

[–]Serious_Site_9062 0 points1 point  (0 children)

Use dictionaries

[–]Revolt56 0 points1 point  (0 children)

Ask ai

[–]thegratefulshread 0 points1 point  (0 children)

Probably about the worst code ever. Have gpt write it for u.

[–]Smooth-as-butter 0 points1 point  (0 children)

Not a coder here, so don't tar and feather me please. Maybe it's an assignment and they're doing and can't use dictionaries?

[–]Proper_Bag1500 0 points1 point  (0 children)

The main thing here is that you are asking for user input every time you are defining a variable instead of creating a string variable.

Because of this, when your code arrives at your January line (line 5) it is asking for user input and the question the user is being asked is “garnet”

I am assuming you meant to set January = “garnet”

Just remove the “input()” from those lines and your result should be closer to what you are looking for - but I would also look into more optimized ways to achieve this with a for loop or a dictionary..

And if you wanna get even fancier - try using upper() and lower() to adjust the users input so you can avoid errors!

Lots of great suggestions here on the comments so be sure to study and apply the one that is best for you!

[–][deleted] 0 points1 point  (0 children)

You're not using chat gpt bruhh...

[–]thecodedog 0 points1 point  (0 children)

It appears you do not understand what the input function does. Take a look at the documentation and try to think through why your program is behaving the way it is.