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

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日

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源