我正在為我目前正在開發的游戲開發遭遇系統。stats 和 medkits 只是為了在完成游戲之前構建系統的占位符,除了這個問題之外,它的功能很好。我希望在循環內訪問 medkits 變量,然后,如果有第二次遇到,將該變量更改為正確的數字。例如,如果我在第一次遭遇時使用了一個醫療包,那么下次我應該少一個。不知道如何做到這一點,因為這是我第一次真正嘗試制作合法游戲。任何功能解決方案都會很棒!我還想補充一點,我知道第三個選項不起作用,敵人還沒有反擊,但這對我來說沒有問題。我只想要這個特定問題的答案。charisma = int(60)strength = int(25)endurance = int(40)intelligence = int(70)capacity = int(50)from random import randintmedkits = 2def encounter(enemy): print("Engaging "+enemy+"!") print() y = 10 + (strength*.10) enhealth = int(100) enhealth = int(enhealth-y) health = int(100) med = int(medkits) while True: print("Pick your move!") print("1. Smack with shovel") print("2. Use a medkit (you have "+str(med)+" medkits)") print("3. Attempt escape") enc = input("I will try to (put a #): ") if "1" in enc: x = randint(0, 100) if x < 80: enhealth = int(enhealth-y) print() print("You hit "+enemy+" for "+str(y)+" damage!") print() else: print() print("Miss!") print() elif "2" in enc: if med > 0: print() print("You used a medkit!") print() health = health+30 med = med-1 print("Health is at "+str(health)) print() else: print("You're out of medkits!") if enhealth <= int(0): print(enemy+" has been defeated! Well done!") breakencounter("Cornelius")
如何訪問并隨后更改循環外的變量
喵喔喔
2021-11-02 15:06:41