答疑代码问题

千里之行始于足下:你好,有一个问题想请教一下可以吗?Python 的。

悦创:先发吧 有时间我看看,我还没放假

import random
import time
enemy_life=100
player_life=100


print('这是一个完全看运气的游戏。真的')
while True :
    while True:
        caculate=random.randint(0,5)
        player_attack=int(input('请输入一个0-30的随机数:'))
        if 0<=player_attack<=30:
            if caculate==1:
                enemy_life=enemy_life-player_attack
                break
            elif caculate==2:
                enemy_life=enemy_life+player_attack
                break
            elif caculate==3:
                enemy_life=enemy_life*player_attack
                break
            elif caculate==4:
                enemy_life=enemy_life//player_attack
                break
        else:
            print('输入错误。请认真审题')
            continue
    enemy_attack=random.randint(0,30)
    player_life=player_life-enemy_attack
    print('Hold on...')
    time.sleep(1)
    print('你的生命值是'+str(player_life))
    print('Hold on...')
    time.sleep(1)


    if enemy_life<=1:
        print('少侠好刀法!敌人死于'+str(enemy_life)+'生命值')
        print('you are winenr')
        break
    elif player_life<=0:
        print('you lose the game')
        break
    elif enemy_life>0:
        print('敌人一息尚存,他还有'+str(enemy_life)+'生命值')
        continue

print('The game is over.But Don Quixote is forever')

 

答疑代码问题答疑代码问题

千里之行始于足下:一个很初级的问题,你抽空看看,有时间回复即可。
也没报错,但是就会这样子,不懂为什么

代码允许中死在 whlie 循环里面,if 外面添加个 break 试一试。

import random
import time

enemy_life = 100
player_life = 100

print('这是一个完全看运气的游戏。真的')
while True:
    while True:
        caculate = random.randint(0, 5)
        player_attack = int(input('请输入一个0-30的随机数:'))
        if 0 <= player_attack <= 30:
            if caculate == 1:
                enemy_life = enemy_life - player_attack
                break
            elif caculate == 2:
                enemy_life = enemy_life + player_attack
                break
            elif caculate == 3:
                enemy_life = enemy_life * player_attack
                break
            elif caculate == 4:
                enemy_life = enemy_life // player_attack
                break
            break
        else:
            print('输入错误。请认真审题')
            continue
    enemy_attack = random.randint(0, 30)
    player_life = player_life - enemy_attack
    print('Hold on...')
    time.sleep(1)
    print('你的生命值是' + str(player_life))
    print('Hold on...')
    time.sleep(1)

    if enemy_life <= 1:
        print('少侠好刀法!敌人死于' + str(enemy_life) + '生命值')
        print('you are winenr')
        break
    elif player_life <= 0:
        print('you lose the game')
        break
    elif enemy_life > 0:
        print('敌人一息尚存,他还有' + str(enemy_life) + '生命值')
        continue

print('The game is over.But Don Quixote is forever')
上一篇:HDU6989 Didn't I Say to Make My Abilities Average in the Next Life?!


下一篇:Managing Your App‘s Life Cycle(管理应用程序的生命周期)