2301_76737700 2023-03-27 13:23 采纳率: 100%
浏览 41
已结题

if else 语句使用break为什么提示在循环外

佬们,为什么我的最后的代码中break提示在循环外


import random
global hand
hand = int(1000)

def winner(point1,point2,point3,bet):
    global hand
    if(point1+point2+point3 >= 11):
        if(big_or_small == 'big'):
            print('You Win!!!')
            hand = hand + bet
        else:
            print('You Lose!!!')
            hand = hand - bet
    else:
        if (big_or_small == 'small'):
            print('You Win!!!')
            hand = hand + bet
        else:
            print('You Lose!!!')
            hand = hand - bet



def bet(hand):
    print('You have ', hand ,'now')
    bet = int(input('How much you wanna bet--- ?\n'))
    if bet > hand:
        print('You bet is error')
    elif bet < 0:
        print('How much you have now??\nGame Over')
    else:
        print('Your bet is begin')
        print('<<<<< ROLE THE DICE!>>>>>')
        print('The points is[', point1, point2, point3, ']!!!!!')
        winner(point1, point2, point3,bet)

point1 = random.randrange(1,7)
point2 = random.randrange(1,7)
point3 = random.randrange(1,7)

print('<<<<< GAME STARTS! >>>>>')
big_or_small = input('Big or Small?\n')
bet(hand)
print('You have ', hand ,'now')
if hand > 0:
    print('Do you want play again')
    print('<<<<< GAME STARTS! >>>>>')
    big_or_small = input('Big or Small?\n')
    bet(hand)
else :
    print('You cant play this game anymore')
    break
  • 写回答

4条回答 默认 最新

  • a5156520 2023-03-27 13:32
    关注

    最后的break不需要吧,然后break是用循环中的,如while循环或for循环,去掉最后的break,然后把if可以改为while,其他地方也可以修改下。

    修改如下:

    参考链接:

     
     
    import random
    global hand
    
    hand = int(1000)
    
     
    def winner(point1,point2,point3,bet):
        global hand
        if(point1+point2+point3 >= 11):
            if(big_or_small == 'big'):
                print('You Win!!!')
                hand = hand + bet
            else:
                print('You Lose!!!')
                hand = hand - bet
        else:
            if (big_or_small == 'small'):
                print('You Win!!!')
                hand = hand + bet
            else:
                print('You Lose!!!')
                hand = hand - bet
     
     
     
    def bet(hand):
        print('You have ', hand ,'now')
        bet = int(input('How much you wanna bet--- ?\n'))
        point1 = random.randrange(1,7)
        point2 = random.randrange(1,7)
        point3 = random.randrange(1,7)
        if bet > hand:
            print('You bet is error')
        elif bet < 0:
            print('How much you have now??\nGame Over')
        else:
            print('Your bet is begin')
            print('<<<<< ROLE THE DICE!>>>>>')
            print('The points is[', point1, point2, point3, ']!!!!!')
            winner(point1, point2, point3,bet)
     
    
     
    print('<<<<< GAME STARTS! >>>>>')
    big_or_small = input('Big or Small?\n')
    bet(hand)
    print('You have ', hand ,'now')
    while hand > 0:
        print('Do you want play again')
        print('<<<<< GAME STARTS! >>>>>')
        
        big_or_small = input('Big or Small?\n')
        bet(hand)
    
    print('You cant play this game anymore')
    #break 这个break不需要
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月4日
  • 已采纳回答 3月27日
  • 创建了问题 3月27日

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急