Home > Net >  Ask user again or exit the loop
Ask user again or exit the loop

Time:12-30

Im trying to ask user for input to choose soda in loop but i want to be able to ask user what soda user want to buy then loop and ask user again or exit (This is what i have problem with need help.

i have tried changing the While True loop to if statements instead but with no luck...

    def single_bottles():
  prices = {"Coca Cola": 16,    "Pepsi": 14,    "Fanta": 16,     "7up": 14,     "Sprite": 13,    "Mt Dew": 16,    "Rasberrysoda": 11,     "Orangina": 12,     "Zingo": 14,    "Pearsoda": 14,    "Pommac": 16,    "Jaffa": 14}

  total = 0

  print('Sodalist\n========')  
  for keys, values in prices.items():    
    print(f'{keys} : {values} kr')    
    
  while True:      
    inp = input('\nWrite name of the soda you want to buy: ').capitalize()      
    try:        
      total  = prices[inp]      
    except:        
      break  
  print(f'Total price : {total}')  

CodePudding user response:

Changes made:-

(1) .get() method is used to add in total dictionary.get(keyname, value) A value can be specified to return if the specified key does not exist. [i.e intialise to 0 in your case]

(2) If someone enters Soda name which not present in our dictionary user will get to know by the message!!

(3) Code will runs till user not input Exit/exit

Code:-

def single_bottles():
    prices = {"Coca Cola": 16,    "Pepsi": 14,    "Fanta": 16,     "7up": 14,     "Sprite": 13,    "Mt Dew": 16,    "Rasberrysoda": 11,     "Orangina": 12,     "Zingo": 14,    "Pearsoda": 14,    "Pommac": 16,    "Jaffa": 14}
    total = 0
    print('Sodalist\n========')  
    
    for keys, values in prices.items():    
        print(f'{keys} : {values} kr')    
    inp=""
    while inp!="Exit":      
        inp=input('\nWrite name of the soda you want to buy or write exit/Exit for billing: ').capitalize()
        total  = prices.get(inp,0)
        if not prices.get(inp,0) and inp!="Exit":
            print("Sorry this Soda is not available")
    print(f'Total price : {total}') 

single_bottles()

Output:-

Sodalist
========
Coca Cola : 16 kr
Pepsi : 14 kr
Fanta : 16 kr
7up : 14 kr
Sprite : 13 kr
Mt Dew : 16 kr
Rasberrysoda : 11 kr
Orangina : 12 kr
Zingo : 14 kr
Pearsoda : 14 kr
Pommac : 16 kr
Jaffa : 14 kr

Write name of the soda you want to buy or write exit/Exit for billing: zingo
Write name of the soda you want to buy or write exit/Exit for billing: perry
Sorry this Soda is not available

Write name of the soda you want to buy or write exit/Exit for billing: jaffa
Write name of the soda you want to buy or write exit/Exit for billing: exit
Total price : 28

CodePudding user response:

If I understood your question correctly, you have to ask the user whether to buy more soda's or to exit right?

If then,

def single_bottles():
    prices = {"Coca Cola": 16,    "Pepsi": 14,    "Fanta": 16,     "7up": 14,     "Sprite": 13,    "Mt Dew": 16,    "Rasberrysoda": 11,     "Orangina": 12,     "Zingo": 14,    "Pearsoda": 14,    "Pommac": 16,    "Jaffa": 14}

    total = 0

    print('Sodalist\n========')  
    for keys, values in prices.items():    
        print(f'{keys} : {values} kr')    

    while True:      
        inp = input('\nWrite name of the soda you want to buy/Q to exit: ').capitalize()      
        if inp in prices:       
            total  = prices[inp]
        elif inp in ['q', 'q'.upper()]:
            break
        else:
            print("Invalid Input, try again.....")
    print(f'Total price : {total}')  

single_bottles()

Output:

Sodalist
========
Coca Cola : 16 kr
Pepsi : 14 kr
Fanta : 16 kr
7up : 14 kr
Sprite : 13 kr
Mt Dew : 16 kr
Rasberrysoda : 11 kr
Orangina : 12 kr
Zingo : 14 kr
Pearsoda : 14 kr
Pommac : 16 kr
Jaffa : 14 kr

Write name of the soda you want to buy/Q to exit: Fanta

Write name of the soda you want to buy/Q to exit: Zango
Invalid Input, try again.....

Write name of the soda you want to buy/Q to exit: Zingo

Write name of the soda you want to buy/Q to exit: q
Total price : 30

CodePudding user response:

Your code is fine. Just call the single_bottles() function after definition:

def single_bottles():
    # Your code
single_bottles()

CodePudding user response:

The best way to do this is to change While True to something else.

If you want the loop to stop when an empty soda name is inputted, then what you can do is do "While inp".

However this change has to be made:

  #THIS HAS TO BE ADDED: 
  inp = "blank"
  #THIS ALSO NEEDS TO BE HERE INSTEAD: 
  while inp:      
     inp = input('\nWrite name of the soda you want to buy: ').capitalize()      
     try:        
       total  = prices[inp]      
     except:        
       continue
  print(f'Total price : {total}')

If you want to make it when Exit is input:

  while inp.lower() != "exit":      
     inp = input('\nWrite name of the soda you want to buy: ').capitalize()      
     try:        
       total  = prices[inp]      
     except:        
       continue
  print(f'Total price : {total}')
  • Related