При попытке использовать оператор "+ =" не добавляет к переменной - PullRequest
0 голосов
/ 07 мая 2020

Я пытаюсь сделать так, чтобы, когда пользователь или бот выигрывали раунд, они получали очко, но в конце игры, кажется, не добавляется, когда один или другой делает это правильно, я попробовал другой way ('x = x + 1'), также оцените мой код и скажите, что я мог бы сделать лучше.

import random

print('Lets play Rock Paper Scissors')

for tries in range (1,4):

    try:
        user_guess = input('Rock Paper Scissors? ')
        choices = ['Rock','Paper','Scissors']
        user_point = 0     #To keep track of the points 
        bot_point = 0     
        bot_guess  = random.choice(choices) #Will randomly pick from the list 'choices'

        while user_guess not in choices:#if the user tries to put in anything other then the choices given
            print('Please enter the choices above!')
            user_guess = input('Rock Paper Scissors? ')
    except ValueError:
        print('Please choose from the choices above ')  #Just in case user tries to put a different value type 
        user_guess = input('Rock Paper Scissors? ')

    DEBUG = "The bot did " + bot_guess

    print(DEBUG)

    if user_guess == bot_guess:
        print('Tie!')

    elif user_guess == "Rock" and bot_guess == "Paper":
        print('The bot earns a point!')
        bot_point += 1 

    elif user_guess == 'Paper' and bot_guess == "Rock":
        print('The user earns a point!')
        user_point += 1 

    elif user_guess == 'Paper' and  bot_guess == 'Scissors':
        print('The bot earns a point')
        bot_point += 1 

    elif user_guess == 'Scissors' and bot_guess == 'Paper':
        print('The user earns a point')
        user_point += 1 

    elif user_guess == 'Rock' and  bot_guess == 'Scissors':
        print('The user earns a point')
        user_point += 1 

    elif user_guess == 'Scissors' and bot_guess == 'Rock': 
        print('The bot earns a point')
        bot_point += 1




print('After ' + str(tries) + ' tries. ' + ' The score is')
print('The User: ' + str(user_point))
print('The Bot: ' + str(bot_point))

if user_point > bot_point:
    print('THE USER IS THE WINNER!!!')
else:
    print('THE BOT IS THE WINNER!!!')

1 Ответ

1 голос
/ 07 мая 2020

Вам необходимо инициализировать user_point и bot_point перед запуском for l oop. На самом деле они сбрасываются в ноль каждый раз через l oop.

...