繁体   English   中英

虽然循环没有中断

[英]While Loop isnt breaking

当我运行程序时它会一遍又一遍地循环,即使game rounds数等于rounds数尝试了不同的方法但仍然有问题或者只有一些我看不到的东西

继承人的代码:


print('Rock Paper Scissors!\n"r" for rock, "p" for paper, "s" for scissors\n')
rounds = int(input('How many rounds?: '))
game_round = 0
game = game_round != rounds
while game:
    user = input('\nRock, Paper, Scissors?: ').lower()
    computer = random.choice(['r', 'p', 's'])
    print(f'Computer: {computer}\n')
    player = user
    opponent = computer
    user_win = 0

    # r > s, s > p, p > r
    # if player wins:
    player_win = (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r')
    # if computer wins:
    computer_w = (opponent == 'r' and player == 's') or (opponent == 's' and player == 'p') or (opponent == 'p' and player == 'r')

    if user == computer and game:
        game_round = game_round + 1
        print('Tie!')

    if player_win and game:
        user_win = user_win + 1
        game_round = game_round + 1
        print('You win!')

    if computer_w and game:
        game_round = game_round + 1
        print('You loose!')

    if game is False:
        print(f'Score {user_win}/{rounds}')```

正如 Sayse 在评论中所说,你可以这样做:

user_win = 0 # <- put user_win outside the loop so you can access it outside the loop.

while game_round != round:
   # other code

print(f"Score {user_win}/{rounds}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM