如何解决我的“石头,剪刀,剪刀”游戏未显示的得分计数?

时间:2019-01-24 13:04:14

标签: python python-3.x

我本周刚刚开始学习编程,并制作了摇滚,剪纸,剪刀游戏。但是,有一个问题:当我绘画时,分数计数显示为:

username: 0, Bot: 0.

但是当我输赢时,分数计数根本没有出现,尽管游戏在没有正确分数的情况下仍能完美运行。

import random
user_score = 0
bot_score = 0

def game(username, user_choice):
    options = ['rock', 'paper', 'scissors']
    bot = random.choice(options)

global user_score
global bot_score

if user_choice == 'rock' and bot == 'scissors':
    print(username + ' played rock, I played scissors. You won. Nice!')
    user_score += 1
    return user_score
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice == 'rock' and bot == 'paper':
    print(username + ' played rock, I played paper. You lost. Haha, loser!')
    bot_score += 1
    return bot_score
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice == 'paper' and bot == 'scissors':
    print(username + ' played paper, I played scissors. You lost. Haha, loser!')
    bot_score += 1
    return bot_score
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice == 'paper' and bot == 'rock':
    print(username + ' played paper, I played rock. You won. Nice!')
    user_score += 1
    return user_score
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice == 'scissors' and bot == 'paper':
    print(username + ' played scissors, I played paper. You won. Nice!')
    user_score += 1
    return user_score
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice == 'scissors' and bot == 'rocks':
    print(username + ' played scissors, I played rocks. You lost. Haha, loser!')
    bot_score += 1
    return bot_score
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice == bot:
    print("It's a draw, dang it!")
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
elif user_choice != 'scissors' and user_choice != 'paper' and user_choice != 'rock':
    print('Please enter a valid choice!')  
    print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))


print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
name = input()

while True:  
    choice = input('Rock, Paper or Scissors?\n').lower().strip()
    game(name, choice)
    if input('Want to play again? Yes or No\n').lower().strip() == 'no':
        print('Goodbye. Press Enter to exit.' + 'Result: User: ' + user_score +' \nBot: '+bot_score)
        input()
        break 

预期:得分计数有效,每当一方获胜时,将user_score和bot_score加1。
实际:当用户输赢时,得分计数不会显示。

2 个答案:

答案 0 :(得分:1)

这只是您忽略的一件事

 return user_score
print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))

如您所见,您已将return语句放置在print语句之前,因此print语句将被忽略,仅返回值。只需进行交互即可对其进行纠正。

print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))
 return user_score

希望有帮助

答案 1 :(得分:-2)

return bot_score
print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))

在打印之前您要返回。 返回值跳出该功能,因此将永远无法达到打印效果。 在您返回之前进行打印时,它应该可以工作