社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

我的python camel游戏的变量不是udating

Nathan Wimmer • 7 年前 • 598 次点击  

我是计算机科学学位的大一新生,我被派去完成这个骆驼游戏。在移动后检查状态时,我似乎找不到更新变量的方法。他们总是打印出旅行的英里数是0,而当地人落后了-20英里。感谢您的帮助! 这是我的密码。

# The comments are from my friend who helped me fix the while loop from repeating over and over

def main ():
    global executed
    executed = 1
    if executed == 1: # This will only show the intro once
        print("Welcome to Camel!")
        print("You have stolen a camel to make your way across the great Mobi desert.")
        print("The natives want their camel back and are chasing you down!")
        print("Survive your desert trek and out run the natives.")
    global done  # Add global next to a variable if you're calling it from outside the function
    executed = 0

main()

done = False
while not done:

    # Game variables
    miles_traveled = int(0)
    thirst = int(0)
    camel_tiredness = int(0)
    native_distance = int(-20)
    canteen_drinks = int(3)

    print("A. Drink from your canteen.")
    print("B. Ahead moderate speed.")
    print("C. Ahead full speed.")
    print("D. Stop for the night.")
    print("E. Status check.")
    print("Q. Quit.")

    user_choice = (input("What is your choice? "))
    # Quit the game
    if user_choice.upper() == "Q":
        done = True
        print("Quitting...")
    # Status check
    elif user_choice.upper() == "E":
        print("Miles traveled:", miles_traveled)
        print("Drinks in Canteen:", canteen_drinks)
        print("The natives are", native_distance, "miles behind you.")
    # Stopping for the night
    elif user_choice.upper() == "D":
        camel_tiredness = int(0)
        print("Your camel is happy.")
        import random
        native_distance += random.randint(7, 14)
        print("The natives are", native_distance, "miles behind you.")
    #Full speed
    elif user_choice.upper() == "C":
        thirst += int(1)
        camel_tiredness += int(1)
        import random
        miles_traveled += random.randint(10, 20)
        print("You have traveled", miles_traveled, "miles.")
        import random
        native_distance += random.randint(7, 14)
    # Moderate speed
    elif user_choice.upper() == "B":
        import random
        miles_traveled += random.randint(5, 12)
        print("You have traveled", miles_traveled,"miles.")
        thirst += int(1)
        camel_tiredness += int(1)
        import random
        native_distance += random.randint(7, 14)
     # Drinking from canteen
    elif user_choice.upper() == "A":
        if canteen_drinks >= 1:
            print("You have taken a sip from your canteen")
            print("You have", canteen_drinks - int(1), "sips left in your canteen.")
        elif canteen_drinks == 0:
            print("You are out of water.")
    # The other stuff at the end
    if thirst > 4 < 6:
        print("You are thirsty.")

    if thirst > 6:
        print("You have died of thirst!")
        done = True

    if camel_tiredness > 5 < 8:
        print("Your camel is tired.")

    if camel_tiredness > 8:
        print("Your camel has died.")

    if native_distance >= 0:
        print("The natives have caught you.")
        done = True

    if native_distance >= -15:
        print("The natives are getting close!")

    if miles_traveled >= 200:
        print("You have won and got away from the natives!")
        done = True

    import random
    oasis = random.randint(0, 20)

    if oasis == 20:
        print("You have found an oasis.")
        thirst = 0
        camel_tiredness = 0
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43061
文章 [ 1 ]  |  最新文章 7 年前
Rarblack
Reply   •   1 楼
Rarblack    7 年前

您的变量应该不在while循环中,这样就不会一遍又一遍地得到相同的值,而且您还应该每次都更新食堂饮料变量。

更新代码

def main ():
    global executed
    executed = 1
    if executed == 1: # This will only show the intro once
        print("Welcome to Camel!")
        print("You have stolen a camel to make your way across the great Mobi desert.")
        print("The natives want their camel back and are chasing you down!")
        print("Survive your desert trek and out run the natives.")
    global done  # Add global next to a variable if you're calling it from outside the function
    executed = 0

main()
import random

done = False

miles_traveled = int(0)
thirst = int(0)
camel_tiredness = int(0)
native_distance = int(-20)
canteen_drinks = int(3)
while not done:

    # Game variables


    print("A. Drink from your canteen.")
    print("B. Ahead moderate speed.")
    print("C. Ahead full speed.")
    print("D. Stop for the night.")
    print("E. Status check.")
    print("Q. Quit.")

    user_choice = (input("What is your choice? "))
    # Quit the game
    if user_choice.upper() == "Q":
        done = True
        print("Quitting...")
    # Status check
    elif user_choice.upper() == "E":
        print("Miles traveled:", miles_traveled)
        print("Drinks in Canteen:", canteen_drinks)
        print("The natives are", native_distance, "miles behind you.")
    # Stopping for the night
    elif user_choice.upper() == "D":
        camel_tiredness = int(0)
        print("Your camel is happy.")
        native_distance += random.randint(7, 14)
        print("The natives are", native_distance, "miles behind you.")
    #Full speed
    elif user_choice.upper() == "C":
        thirst += int(1)
        camel_tiredness += int(1)

        miles_traveled += random.randint(10, 20)
        print("You have traveled", miles_traveled, "miles.")
        native_distance += random.randint(7, 14)
    # Moderate speed
    elif user_choice.upper() == "B":
        miles_traveled += random.randint(5, 12)
        print("You have traveled", miles_traveled,"miles.")
        thirst += int(1)
        camel_tiredness += int(1)
        native_distance += random.randint(7, 14)
     # Drinking from canteen
    elif user_choice.upper() == "A":
        if canteen_drinks >= 1:
            print("You have taken a sip from your canteen")
            canteen_drinks = canteen_drinks - int(1) # you should update the centeen_drinks
            print("You have", canteen_drinks, "sips left in your canteen.")
        elif canteen_drinks == 0:
            print("You are out of water.")
    # The other stuff at the end
    if thirst > 4 < 6:
        print("You are thirsty.")

    if thirst > 6:
        print("You have died of thirst!")
        done = True

    if camel_tiredness > 5 < 8:
        print("Your camel is tired.")

    if camel_tiredness > 8:
        print("Your camel has died.")

    if native_distance >= 0:
        print("The natives have caught you.")
        done = True

    if native_distance >= -15:
        print("The natives are getting close!")

    if miles_traveled >= 200:
        print("You have won and got away from the natives!")
        done = True

    oasis = random.randint(0, 20)

    if oasis == 20:
        print("You have found an oasis.")
        thirst = 0
        camel_tiredness = 0