Python骆驼游戏麻烦

时间:2015-03-24 16:39:53

标签: python-3.x

我刚开始在网站上使用教程学习python。一个实验室让我创造了这个骆驼游戏,我知道它仍然是不完整的。现在我正试图弄清楚如何打印当地人背后的里程数而不是负数(因为这没有意义)。我知道代码中可能还有其他错误,但现在我正专注于这个问题。

提前致谢!这是代码:

import random

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! Survive your \n"
  "desert trek and out run the natives.")
print()

done = False

# VARIABLES
miles_traveled = 0
thirst = 0
tiredness = 0
natives_travel = -20
canteen = 5
forward_natives = random.randrange(0, 10)
full_speed = random.randrange(10, 20)
moderate_speed = random.randrange(5, 12)
oasis = random.randrange(0, 21)

# MAIN PROGRAM LOOP

while not done:
    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.")

    choice = input("Your choice? ")
    print()
    if choice.upper() == "Q":
        done = True
        print("You quit!")

    # STATUS CHECK
    elif choice.upper() == "E":
        print("Miles traveled: ", miles_traveled)
        print("Drinks in canteen: ", canteen)
        print("The natives are", (natives_travel + 20), "miles behind you.")
        print()

    # STOP FOR THE NIGHT
    elif choice.upper() == "D":
        tiredness = 0
        print("The camel is happy")
        natives_travel = natives_travel + forward_natives
        print("The natives are", natives_travel, "miles behind you.")
        print()

    # FULL SPEED AHEAD
    elif choice.upper() == "C":
        if oasis == 1:
            print("Wow you found an oasis")
            canteen = 5
            thirst = 0
            tiredness = 0

    else:
        miles_traveled = miles_traveled + full_speed
        print("You walked", full_speed, "miles.")
        thirst += 1
        tiredness = tiredness + random.randrange(1, 4)
        natives_travel = natives_travel + forward_natives
        print(forward_natives)
        print("The natives are", natives_travel, "miles behind you.")
        print()

    # MODERATE SPEED
    elif choice.upper() == "B":
        if oasis == 1:
        print("Wow you found an oasis")
        canteen = 5
        thirst = 0
        tiredness = 0

    else:
        miles_traveled = miles_traveled + moderate_speed
        print("You walked", moderate_speed, "miles")
        thirst += 1
        tiredness = tiredness + random.randrange(1, 3)
        natives_travel = natives_travel + forward_natives
        print("The natives are", natives_travel, "miles behind you.")
        print()

    # DRINK FROM CANTEEN
    elif choice.upper() == "A":
        if canteen >= 1:
            canteen -= 1
            thirst = 0
    if canteen == 0:
        print("You have no drinks in the canteen!")
        print()

    if miles_traveled >= 200:
        print("You have won!")
        done = True

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

    if not done and thirst >= 4:
        print("You are thirsty.")
        print()

    if not done and tiredness >= 8:
        print("Your camel is dead")
        done = True
        print()

    if not done and tiredness >= 5:
        print("Your camel is getting tired.")
        print()

    if natives_travel >= miles_traveled:
        print("The natives have caught you!")
        done = True
        print()

    elif natives_travel <= 15:
        print("The natives are getting close!")
        print()

1 个答案:

答案 0 :(得分:0)

使用函数abs()作为绝对值。或者,如果此距离始终为负,则只需...使用减号?

相关问题