Else statement is not geting after many uses of if and else statement

**Question:**else statement is not geting after many uses of if and else statement


Repl link: https://replit.com/@TanayTs/First?s=app

code snippet

Hi, @TanayTs please be much more precise about what is not working.

Looking through your code you have a few obvious issues. When you are doing comparisons such as name == ("1"), this will not work as you expect as it is trying to compare the String name to a Tuple, instead you should do name == "1". Also, your final if statement will not work as expected, name == ("1") or ("ONLINE") or ("online") will always be true, instead you need to do name == "1" or name == "ONLINE" or name == "online". Also, I would suggest using elifs to make the elses work correctly. ALSO, you need to indent your code. AND, you should also be naming your variables with appropriate names

I’d be looking at changing your code to something like this:

print("Hello world! welcome to my pc shop!!!!")

name = input("what is your name?\n")
print("Welcome " + name +  " This is my new pc shop here you  can build a costom pc or buy a pre-built pc\n")

build_type = input("Choose the option\n 1.Pre-built\n 2.Costom pc\n")

if build_type == "Pre-built":  
  cpu = input("Choose the CPU(write in capital letters)\n INTEL\n AMD\n")  
  
  if cpu == "INTEL":
    spec = input(" choose the pre-built pc from down \n 1. RAMP\n SPEC'S\n INTEL CORE i5 12600k\n 16GB DDR5 RAM\n 1TB SSD GEN4\n TOWER CPU COOLER\n RTX 3060ti\n 2. ROCKY\n SPEC'S\n INTEL CORE i7 12600k\n 32GB DDR5 RAM\n 4TB SSD GEN4\n AIO CPU COOLER\n RTX 4080ti\n")
    
    if spec == "1":
      print("price = 1.2lakh's\n")
    elif spec == "2":
      print("price = 1.10 lakh's\n")
      
  elif cpu == "AMD":    
    spec = input("CHOOSE THE PRE-BUILT PC FROM DOWN (CHOOSE BY SERIAL NUMBER)\n 1.AMP\n\n SPEC'S\n\n AMD RYZEN 5 5600\n 8GB RAM DDR4\n 512gb hdd and 128gb ssd\n AMD stock cooler\n no gpu included\n\n\n 2.AMSD\n\n AMD 7 16gb RAM DDR4\n 1tb HDD AND 512GB SSD\n TOWER CPU COOLER\n RTX 1080ti\n")
    
    if spec == "1":
      print("price = 60,000\n")
    elif spec == "2":
      print("price = 1 lakh\n")


payment = input("select the payment method\n 1. ONLINE\n 2.OFFLINE\n")
if payment == "1" or payment == "ONLINE" or payment == "online":
  mobile = input("Please enter your mobile number\n")
elif payment == "2" or payment == "OFFLINE" or payment == "offline":
  something_else = input("please enter your")

print("THANK YOU FOR SPENDING TIME WITH US YOUR ORDER WILL BE DELIVERIED IN 2 WEEKS")
1 Like

The reason why the program is not running the else statement is because your if statements will always be true, since you are testing always-truthy values inside.

If you want to test multiple values against a variable, do something like this:

x = 4
if x in {5, 2, 3}:
     # this will return false
elif x in {8, 6, 4}:
     # this will return true
else:
    # this will return false

Hope this helps!

1 Like

You might have missed one of them there cactus…