python - 如何修复我的石头、纸、剪刀游戏的分数计数不显示?
问题描述
我这周刚开始学习编程,做了一个石头剪刀布游戏。但是,有一个问题:当我绘制时,分数显示:
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。
实际:用户输赢时不显示得分计数。
解决方案
它只是你忽略的一件事
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
希望能帮助到你
推荐阅读
- java - 尝试将文本文件作为参数传递时出错
- javascript - Jquery获取选中的项目返回未选中的框?
- javascript - VueJS:使用axios获取数据后未定义数组的属性
- python - 计算长度为 n 的随机数的实例
- python - Excel 文件未保存在 heroku 服务器上(Flask 应用程序)
- javascript - 一次激活一个按钮的切换
- envoyproxy - lua envoyFilter 在 istio-sidecar 中被忽略
- azure-devops - Azure DevOps REST API 获取请求不起作用
- node.js - 集成请求 aws api-getway 中的正确模板映射应该是什么?
- excel - 如何在不拖动的情况下使用自动更新公式