0
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300", "Bloodcursed Sword: 160 Damage, Cost 800"]
shopitemsM = ["Fire Throw: 150 Damage, Cost: 700", "Ice Wind: 120 Damage, Cost: 300", "Electric shock: 160 Damage, Cost 800"]
print("Welcome to the shop.")
print('')
if character == "Fighter":
g = ', '
print(g.join(shopitemsF))
time.sleep(1)
elif character == "Mage":
g = ', '
print(g.join(shopitemsM))
time.sleep(1)
shopchoice = input("What would you like to buy? ")
print('')
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
if int(text2[-3:]) >= gold:
print("You need another", int(text2[-3:]) - gold, "gold.")
elif int(text2[-3:]) <= gold:
print("You have purchased,", text2[:-11]+".")
x = (int(text2[-21:-18]))
for text2 in shopitemsM:
if shopchoice in text2:
print(text2)
if int(text2[-3:]) >= gold:
print("You need another", int(text2[-3:]) - gold, "gold.")
elif int(text2[-3:]) <= gold:
print("You have purchased,", text2[:-11]+".")
x = (int(text2[-21:-18]))
我想它所以一旦代碼到達if語句如何使if語句回到以前的輸入代碼
if int(text2[-3:]) >= gold:
print("You need another", int(text2[-3:]) - gold, "gold.")
這將重新顯示「你想什麼購買?「輸入,以便他們能夠繼續選擇一個項目,直到他們選擇一個他們可以負擔得起的項目並將導致elif語句運行。就像我想象的那樣,我需要一個while循環,但由於這個代碼實例,我不確定要做什麼。
我使用了你所做的break語句部分,並添加到了shopchoice變量中,以重新提出if語句下面的問題。哪個工作是好的,但是如果elif語句被稱爲無限循環。我不確定爲什麼。 – mykill456