Skip to content
Advertisement

repeating a program using while loop failed

I coded a program that is meant to repeat itself over and over until the to_continue loop is broken by the input “N”. However, it didn’t seem to work.

phone_or_tablet=int(input("Phone or Tablet 1/2 "))
while phone_or_tablet not in [1,2]:
  print("Error")
  phone_or_tablet=int(input("Phone or Tablet 1/2 "))
else:
 if phone_or_tablet == 1:
    description=int(input("Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 "))
    while description not in range (1,7,1):
      print("Error")
      description=int(input("Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 "))
#some other programs (scroll down for the whole program)
print(sum(price))
to_continue=input("Continue? Y/N ")
while to_continue.capitalize() == "Y":
  phone_or_tablet=int(input("Phone or Tablet 1/2 "))

Output:

Phone or Tablet 1/2 1
Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 1
#some other stuff
49.98
Continue? Y/N Y
Phone or Tablet 1/2 1
Phone or Tablet 1/2 

whilst I meant for it to output

Phone or Tablet 1/2 1
Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 1
#some other stuff
49.98
Continue? Y/N Y
Phone or Tablet 1/2 1
Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 1

Full program if that’s helpful:

typelst=["Compact", "Clam Shell", "Robo5-in", "Robo6-in", "Y-Standard", "Y-Deluxe"]
pricelst=[29.99,49.99,199.99,499.99,549.99,649.99]
typelst2=["Robo-8in", "Robo-10in", "Y-standard", "Y-deluxe"]
pricelst2=[149.99,299.99,499.99,599.99]
price=[]
phone_or_tablet=int(input("Phone or Tablet 1/2 "))
while phone_or_tablet not in [1,2]:
  print("Error")
  phone_or_tablet=int(input("Phone or Tablet 1/2 "))
else:
  if phone_or_tablet == 1:
    description=int(input("Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 "))
    while description not in range (1,7,1):
      print("Error")
      description=int(input("Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 "))    
    else:
      price.append(pricelst[description-1])
      sim_card=int(input("SIM card free or PAYG? 1/2 "))
      while sim_card not in range (1,3,1):
        print("Error")
        sim_card=input(int("SIM card none, free or PAYG? 0/1/2 "))
      else:
        if sim_card == 2:
          price.append(9.99)
  elif phone_or_tablet == 2:
    description=int(input("Robo-8in, Robo-10in, Y-standard or Y-deluxe? 1/2/3/4 "))
    while description not in range (1,5,1):
      print("Error")
      description=int(input("Robo-8in, Robo-10in, Y-standard or Y-deluxe? 1/2/3/4 "))
    else:
      price.append(pricelst2[description-1])
case=int(input("Standard or Luxury case? 1/2 "))
while case not in range (0,3,1):
  print("Error")
  case=int(input("Standard or Luxury case? 1/2 "))
else:
  if case==2:
    price.append(50)
charger=int(input("Car, Home charger or both? 1/2/3 "))
while charger not in range (0,4,1):
  print("Error")
  charger=int(input("Car, Home charger or both? 1/2/3 "))
else:
  if charger==1:
    price.append(19.99)
  elif charger==2:
    price.append(15.99)
    print(price)
  else:
    price.append(35.98)
print(sum(price))
to_continue=input("Continue? Y/N ")
while to_continue.capitalize() == "Y":
  phone_or_tablet=int(input("Phone or Tablet 1/2 "))

Any help would be deeply appreciated :) Please comment if any of the explanations are unclear or if you need further details

Advertisement

Answer

You have to wrap the whole program logic in one loop.

while True:
  typelst=["Compact", "Clam Shell", "Robo5-in", "Robo6-in", "Y-Standard", "Y-Deluxe"]
  pricelst=[29.99,49.99,199.99,499.99,549.99,649.99]
  typelst2=["Robo-8in", "Robo-10in", "Y-standard", "Y-deluxe"]
  pricelst2=[149.99,299.99,499.99,599.99]
  price=[]
  phone_or_tablet=int(input("Phone or Tablet 1/2 "))
  while phone_or_tablet not in [1,2]:
    print("Error")
    phone_or_tablet=int(input("Phone or Tablet 1/2 "))
  else:
    if phone_or_tablet == 1:
      description=int(input("Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 "))
      while description not in range (1,7,1):
        print("Error")
        description=int(input("Compact, Clam Shell, Robo5-in, Robo6-in, Y-Standard or Y-Deluxe? 1/2/3/4/5/6 "))    
      else:
        price.append(pricelst[description-1])
        sim_card=int(input("SIM card free or PAYG? 1/2 "))
        while sim_card not in range (1,3,1):
          print("Error")
          sim_card=input(int("SIM card none, free or PAYG? 0/1/2 "))
        else:
          if sim_card == 2:
            price.append(9.99)
    elif phone_or_tablet == 2:
      description=int(input("Robo-8in, Robo-10in, Y-standard or Y-deluxe? 1/2/3/4 "))
      while description not in range (1,5,1):
        print("Error")
        description=int(input("Robo-8in, Robo-10in, Y-standard or Y-deluxe? 1/2/3/4 "))
      else:
        price.append(pricelst2[description-1])
  case=int(input("Standard or Luxury case? 1/2 "))
  while case not in range (0,3,1):
    print("Error")
    case=int(input("Standard or Luxury case? 1/2 "))
  else:
    if case==2:
      price.append(50)
  charger=int(input("Car, Home charger or both? 1/2/3 "))
  while charger not in range (0,4,1):
    print("Error")
    charger=int(input("Car, Home charger or both? 1/2/3 "))
  else:
    if charger==1:
      price.append(19.99)
    elif charger==2:
      price.append(15.99)
      print(price)
    else:
      price.append(35.98)
  print(sum(price))
  to_continue=input("Continue? Y/N ")
  if to_continue.capitalize() == "N":
    break
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement