这周我刚开始学习编程,做了一个石头、布、剪刀的游戏。然而,有一个问题:当我抽签时,分数显示:
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
预期:得分计数有效,每次一方获胜时,用户得分和机器人得分加1。
实际:分数计数不显示时,用户赢或输。你知道吗
只是你忽略了一件事
如您所见,您已将return语句放在print语句之前,因此print语句被忽略,只返回值。它可以通过简单的互换来修正。你知道吗
希望有帮助
打印前返回。 返回跳出函数,因此永远无法到达打印。 它应该工作,当你打印之前,你回来
相关问题 更多 >
编程相关推荐