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

最后的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

在Python中,break语句用于立即退出循环,跳转到循环语句之后的第一条语句。在你的代码中,break被用于终止整个程序的执行,但是它被放置在一个没有循环的代码块中。这是不正确的,因为break只能在循环语句中使用。

如果你想要终止while或for循环,请将break放在循环内部。如果你想要在满足某些条件时终止程序的执行,请使用sys.exit()函数,它会终止整个程序的执行。

另外,你的代码中定义了一个bet()函数和一个bet变量。请注意不要使用相同的名称来定义变量和函数,因为这会导致名称冲突和不可预测的行为。建议将函数和变量名称修改为不同的名称。

因为本来就在循环外呀
你从头到尾也根本没有一个循环呀
把break删掉

  • 可以看下python参考手册中的 python- 循环中的 break、continue 语句及 else 子句
  • 除此之外, 这篇博客: 4.15while循环和循环关键字中的 2)break - 结束整个循环 - 当执行循环体的时候遇到了break,整个循环直接结束。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • while循环和break的配合:
    while True:
        需要重复执行的代码
        if 循环结束条件:
            break
        
    
    for x in range(5):
        if x % 2:
            break
        print(x)
    # 0 1
    
    while True:
        value = int(input('请输入一个数:'))
        if value == 0:
            break
    
    num = int(input('请输入任意一个正整数:'))
    count = 0
    
    while True:
        count += 1
        num //= 10
        if num == 0:
            break
    print(count)