循环和忽略标志 - Python

时间:2016-02-07 21:13:23

标签: python loops

我正在研究python,并决定编写一个小型的战斗引擎,使用我学到的一些不同的东西来制作更复杂的东西。我遇到的问题是我设置了一个用户选择应该导致两部分中的一部分加载的选择,而是跳过我的循环而不是执行选择。以下是我到目前为止的情况:

import time
import random 
import sys

player_health = 100
enemy_health = random.randint(50, 110)

def monster_damage():
    mon_dmg = random.randint(5,25)
    enemy_health - mon_dmg
    print ('You hit the beast for ' + str(mon_dmg) + ' damage! Which brings its health to ' + str(enemy_health))
    player_dmg()

def player_dmg():
    pla_dmg = random.randint(5,15)
    player_health - pla_dmg
    print ('The beast strikes out for ' + str(pla_dmg) + ' damage to you. This leaves you with ' + str(player_health))

def run_away():
    run_chance = random.randint(1,10)
    if run_chance > 5:
        print ('You escape the beast!')
        time.sleep(10)
        sys.exit
    else:
        print ('You try to run and fail!')
        player_dmg()

def player_turn():
    print ('Your Turn:')
    print ('Your Health: ' + str(player_health) + ' Monsters Health: ' + str(enemy_health))
    print ('What is your next action?')
    print ('Please Select 1 to attack or 2 to run.')
    action = input()

    if action == 1:
        monster_damage()
    elif action == 2:
        run_away()

while player_health > 0 and enemy_health > 0:
    player_turn()

if player_health <= 0:
    print ('The beast has vanquished you!')
    time.sleep(10)
    sys.exit
elif enemy_health <= 0:
    print ('You have vanquished the beast and saved our Chimichongas')
    time.sleep(10)
    sys.exit

1 个答案:

答案 0 :(得分:0)

函数input返回str而不是int

action = input()

因此,此比较将始终返回False

if action == 1:

例如

>>> '1' == 1
False

您可以将其输入转换为int,如下所示

action = int(input())