If elif else for python confused

Question:

So I’m struggling to get down the coding for an assignment I’m doing I’ve tried so many different ways but I can’t get the code to run correctly.

 brand = (input('Please select a brand: ')) 
 brand1 = "Storm"
 brand2 = "Roto-Grip"

    
  
if brand1:
  model= (input('Please select a model: ')) 
storm_G = "Gravity" 
storm_T = "Tropical" 
storm_H = "Hy-Road" 
storm_E = "Electrify"
    
    
if storm_G:
  color = (input('Please select a color: ')) 
G_fire = "Fire" 
G_crimson ="Crimson" 
if color != {G_fire}:
  if color != {G_crimson}:
    print (f'Sorry, {brand1} {storm_G} is only available in {G_fire} and {G_crimson}.')
    
elif storm_T:
  color = (input('Please select a color: ')) 
T_cherry = "Cherry" 
T_rainbow = "Rainbow" 
if color != {T_cherry} and color != {T_rainbow}:
  print (f'Sorry, {brand1} {storm_T} is only available in {T_cherry} and {T_rainbow}.')

At first, the first section was running fine but then I moved on to the brand’s second model and noticed even if I put the color in correctly the result was the sorry message from both the if and elif statement. Now I can’t even get the first code to run correctly. There are going to be 2 more models with 2 different color choices for each and 4 different models (2 colors each) of the second brand. I just need help with these sorry messages that keep popping up. If the user enters in a model but a color that the model does not have then a sorry message should prompt. As I said earlier once I get to the color input on the output no matter what I do I am prompt with the sorry message for both statements,

"Sorry, Storm Gravity is only available in Fire and Crimson."

"Sorry, Storm Tropical is only available in Cherry and Rainbow."

If there’s anyone that can help it would truly be appreciated.

Asked By: Natawee

||

Answers:

Your if statements for checking brand and model are missing the crucial part: the comparison. The part after the if is checked as a boolean. A string that is not empty will convert to true, therefore your if statement will always be evaluated. You want to compare the input with the provided brands / models like so:

if brand == brand1:
...
if model == storm_G:
...
elif model == storm_T:
...

Also, your if statements for the colors will not work, because you are creating a set when using curly braces. You probably copied that from the print statement. To actually compare the values, it should look like this:

if color != G_fire and color != G_crimson:
  print(f"Sorry...")

Edit: after your comment, I realized the indentation is all over the place. Try to think about when something should be done. For example, the model should only equal storm_G if the brand also equals brand1. Therefore everything should be indented under the if brand == brand1:. Same goes for the colors, they should be indented under the if / elif statements for the models.

The resulting program would look something like this:

brand = input('Please select a brand: ')
brand1 = "Storm"
brand2 = "Roto-Grip"

if brand == brand1:
  model = input('Please select a model: ')
  storm_G = "Gravity" 
  storm_T = "Tropical" 
  storm_H = "Hy-Road" 
  storm_E = "Electrify"
    
  if model == storm_G:
    color = (input('Please select a color: ')) 
    G_fire = "Fire"
    G_crimson ="Crimson" 
    if color != G_fire and color != {G_crimson}:
      print (f'Sorry, {brand1} {storm_G} is only available in {G_fire} and {G_crimson}.')
    
  elif model == storm_T:
    color = (input('Please select a color: ')) 
    T_cherry = "Cherry" 
    T_rainbow = "Rainbow" 
    if color != T_cherry and color != T_rainbow:
      print (f'Sorry, {brand1} {storm_T} is only available in {T_cherry} and {T_rainbow}.')
Answered By: Jeanot Zubler
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.