千里之行始于足下:你好,有一个问题想请教一下可以吗?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')
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。QQ、微信在线,随时响应!V:Jiabcdefh
AI悦创 » 答疑代码问题

发表评论