2017-05-02 40 views
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循環,但由於這個代碼實例,我不確定要做什麼。

回答

0

我只會回答這個問題。我缺乏時間,因此無法對您的計劃進行可能/必要的改進。也許其他答案會提供給他們。如果這部分程序按預期正常工作,可以將其發佈在https://codereview.stackexchange.com上以獲取有關如何改進它的輸入信息。

您可以使用一個布爾變量repeat_shopping來控制while循環:

repeat_shopping=True 
while repeat_shopping: 
    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 
       repeat_shopping=False 


    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])) 
       repeat_shopping=False 

你甚至可以通過使用break語句

while True: 
    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 
       break 


    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])) 
       break 

但沒有這些方法之間的差異避免這個變量。如果使用該變量,則可以在設置變量後執行一些活動(例如,如果變量在第一個if語句中設置,則輸入第二個if語句)。如果使用break語句,while循環將立即離開。

+0

我使用了你所做的break語句部分,並添加到了shopchoice變量中,以重新提出if語句下面的問題。哪個工作是好的,但是如果elif語句被稱爲無限循環。我不確定爲什麼。 – mykill456