【问题标题】:Python camel game troublePython骆驼游戏麻烦
【发布时间】:2015-03-24 16:39:52
【问题描述】:

我刚开始使用网站上的教程学习 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()

【问题讨论】:

标签: python-3.x


【解决方案1】:

使用函数 abs() 获取绝对值。或者,如果这个距离总是负数,就……用减号?

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多