如何修复我的摇滚,纸张,剪刀游戏的分数不显示? [关闭]

问题描述 投票:-6回答:2

我本周刚开始学习编程,并制作了Rock,Paper,Scissors游戏。但是,有一个问题:当我画画时,分数显示:

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。 实际:当用户输赢时,分数不会显示。

python python-3.x
2个回答
1
投票

它只是你忽略的一件事

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

如您所见,您已将return语句放在print语句之前,因此将忽略print语句并仅返回value。它可以通过简单的互换来纠正。

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

希望能帮助到你


-2
投票
return bot_score
print('\n'+username+': '+str(user_score)+', Bot: '+str(bot_score))

你打印之前就回来了。返回跳出函数,因此永远不会达到打印。在您返回之前打印时,它应该可以工作

© www.soinside.com 2019 - 2024. All rights reserved.