python 3.x - How to go inside the loop again -


money =20  items = {'apple': 2, 'banana': 4, 'orange': 6} item_name in items:     print('--------------------------------------------------')      print("you have "+ str(money)+" dollars in wallet")      print('each ' + item_name + ' costs ' + str(items[item_name]) + ' dollars')      input_count = input('how many ' + item_name + 's want?: ')     print('you buy ' + input_count + ' ' + item_name + 's')      count = int(input_count)     total_price = items[item_name] * count     print('the total price ' + str(total_price) + ' dollars')       if money >= total_price:         print("you have bought "+str(input_count)+" "+str(item_name)+"s")          money = money-total_price     elif money < total_price:         print("you not have enough money")         print("you cannot buy many"+str(item_name)+"s")  print(str(money)+" dollars left in wallet") input("do want buy somehing else: ")  if input() =="yes": 

output:

 have 20 dollars in wallet each banana costs 4 dollars how many bananas want?: 2 buy 2 bananas total price 8 dollars have bought 2 bananas ----------------------------------- have 12 dollars in wallet each apple costs 2 dollars how many apples want?: 2 buy 2 apples total price 4 dollars have bought 2 apples ----------------------------------- have 8 dollars in wallet each orange costs 6 dollars how many oranges want?: 1 buy 1 oranges total price 6 dollars have bought 1 oranges 2 dollars left in wallet 

2 dollars left in wallet here still have left 2 dollars , want enter again in loop:

like using print("do want buy else") if user enter yes goto again inside loop if no quit program.

but getting difficulties make this

not tested, think like:

money =20 items = {'apple': 2, 'banana': 4, 'orange': 6}   while true:     item_name in items:         print('--------------------------------------------------')          print("you have "+ str(money)+" dollars in wallet")          print('each ' + item_name + ' costs ' + str(items[item_name]) + 'dollars')          input_count = input('how many ' + item_name + 's want?: ')         print('you buy ' + input_count + ' ' + item_name + 's')          count = int(input_count)         total_price = items[item_name] * count         print('the total price ' + str(total_price) + ' dollars')           if money >= total_price:             print("you have bought "+str(input_count)+""+str(item_name)+"s")              money = money-total_price         elif money < total_price:             print("you not have enough money")             print("you cannot buy many"+str(item_name)+"s")      print(str(money)+" dollars left in wallet") input("do want buy somehing else: ")      if input() != "yes":         break 

this loops content of while loop. break command if enter "yes" jumps out of loop , stops everything.

edit: wait... code changed, while wrote answer. nevertheless, put everthing in while-loop , should work

edit2: fixed mistake, money shouldn't set 20 in each loop, , items don't need reset either.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -