money =20
items = {'apple': 2, 'banana': 4, 'orange': 6}
for item_name in items:
print('--------------------------------------------------')
print("You have "+ str(money)+" dollars in your wallet")
print('Each ' + item_name + ' costs ' + str(items[item_name]) + ' dollars')
input_count = input('How many ' + item_name + 's do you want?: ')
print('You will buy ' + input_count + ' ' + item_name + 's')
count = int(input_count)
total_price = items[item_name] * count
print('The total price is ' + 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 do not have enough money")
print("You cannot buy that many"+str(item_name)+"s")
print(str(money)+" dollars left in your wallet")
input("do want to buy somehing else: ")
if input() =="yes":
输出:
You have 20 dollars in your wallet Each banana costs 4 dollars How many bananas do you want?: 2 You will buy 2 bananas The total price is 8 dollars You have bought 2 bananas ----------------------------------- You have 12 dollars in your wallet Each apple costs 2 dollars How many apples do you want?: 2 You will buy 2 apples The total price is 4 dollars You have bought 2 apples ----------------------------------- You have 8 dollars in your wallet Each orange costs 6 dollars How many oranges do you want?: 1 You will buy 1 oranges The total price is 6 dollars You have bought 1 oranges 2 dollars left in your wallet
你的钱包还剩2美元所以我们还剩2美元,我想再次进入循环:
如果使用print(“你想买别的东西”),如果用户输入yes,那么在for循环内再次转到,如果没有则退出该程序。
但是我很难做到这样
答案 0 :(得分:0)
未经测试,但我认为您可以执行以下操作:
money =20
items = {'apple': 2, 'banana': 4, 'orange': 6}
while True:
for item_name in items:
print('--------------------------------------------------')
print("You have "+ str(money)+" dollars in your wallet")
print('Each ' + item_name + ' costs ' + str(items[item_name]) + 'dollars')
input_count = input('How many ' + item_name + 's do you want?: ')
print('You will buy ' + input_count + ' ' + item_name + 's')
count = int(input_count)
total_price = items[item_name] * count
print('The total price is ' + 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 do not have enough money")
print("You cannot buy that many"+str(item_name)+"s")
print(str(money)+" dollars left in your wallet") input("do want to buy somehing else: ")
if input() != "yes":
break
这会循环while循环的内容。如果输入“yes”,则break命令会跳出循环并停止所有操作。
编辑:等等......代码改变了,而我写了答案。尽管如此,把它放在while-Loop中它应该可行
Edit2:修正了一个错误,每个循环中的钱不应该设置为20,并且这些项也不需要重置。
答案 1 :(得分:0)
将代码放在类方法中并再次调用该类。
答案 2 :(得分:0)
您需要做的是执行for循环,而 条件 true 。幸运的是,Python以while循环的形式提供了这个功能。
money = 20
items = {'apple': 2, 'banana': 4, 'orange': 6}
while money > 0:
for item_name in items:
print('--------------------------------------------------')
print("You have "+ str(money)+" dollars in your wallet")
print('Each ' + item_name + ' costs ' + str(items[item_name]) + ' dollars')
input_count = input('How many ' + item_name + 's do you want?: ')
print('You will buy ' + input_count + ' ' + item_name + 's')
count = int(input_count)
total_price = items[item_name] * count
print('The total price is ' + 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 do not have enough money")
print("You cannot buy that many"+str(item_name)+"s")
print(str(money)+" dollars left in your wallet")
user_input = input("do want to buy something else: ")
if user_input != "yes":
break
for循环完成后,程序会询问用户是否要购买其他东西。如果它们没有钱,则while循环条件将为false,这意味着程序将结束,因为while循环之后没有代码。
希望这有帮助!