我正在用python做石头剪刀布游戏。在其中,用户以及计算机将获得3次机会。赢得回合的人将获得1
分。每回合,积分增加到获胜者。
代码如下:
from random import choice
def stone_paper_scissor(): #g = guess
print("Choose either of the following:")
print(" stone, paper, scissor")
for i in range(3):
guess = ("stone", "paper", "scissor")# guess
user_g = input('ENTER YOUR GUESS:')
user_g.lower()
comp_g = choice(guess) #computer"s guess
comp = 0 # computer's score
user = 0 # user's score
#-------------------------------------------------------------------------
# when both parties guess are the same
if comp_g == user_g:
print( "--------------------------------------" )
print( "IT'S A DRAW!" )
print( "--------------------------------------" )
break
#-------------------------------------------------------------------------
# when computer chooses stone(guess[0])
if comp_g == guess[0]:
if user_g == guess[1]:
print( "----------------------------------" )
print( "YOU WIN!" )
print( "----------------------------------" )
print("COMPUTER"+ comp_g)
print("USER"+ user_g)
user += 1
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))
elif user_g == guess[2]:
print( "----------------------------------" )
print( "YOU LOSE!" )
print( "----------------------------------" )
print("COMPUTER"+ comp_g)
print("USER"+ user_g)
comp += 1
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))
#-------------------------------------------------------------------------
# when computer chooses paper(guess[1])
if comp_g == guess[1]:
if user_g == guess[0]:
print( "----------------------------------" )
print( "YOU LOSE!" )
print( "----------------------------------" )
print("COMPUTER"+ comp_g)
print("USER"+ user_g)
comp += 1
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))
elif user_g == guess[2]:
print( "----------------------------------" )
print( "YOU WIN!" )
print( "----------------------------------" )
print("COMPUTER" + comp_g)
print("USER" + user_g)
user += 1
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))
#---------------------------------------------------------------------------
# when computer chooses scissor (guess[2])
if comp_g == guess[2]:
if user_g == guess[0]:
print( "----------------------------------" )
print( "YOU WIN!" )
print( "----------------------------------" )
print("COMPUTER"+ comp_g)
print("USER"+ user_g)
user += 1
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))
elif user_g == guess[1]:
print( "----------------------------------" )
print( "YOU LOSE!" )
print( "----------------------------------" )
print("COMPUTER"+ comp_g)
print("USER"+ user_g)
comp += 1
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))
#--------------------------------------------------------------------------------
print(stone_paper_scissor())
这是我期望的输出:
Choose either of the following:
stone, paper, scissor
ENTER YOUR GUESS:paper
----------------------------------
YOU WIN!
----------------------------------
COMPUTER:stone
USER:paper
USER: 1
COMPUTER: 0
ENTER YOUR GUESS:scissor
----------------------------------
YOU WIN!
----------------------------------
COMPUTER:paper
USER:scissor
USER: 2
COMPUTER: 0
ENTER YOUR GUESS:paper
----------------------------------
YOU LOSE!
----------------------------------
COMPUTER:scissor
USER:paper
USER: 2
COMPUTER: 1
None
Process finished with exit code 0
但是我得到的输出是不期望的:
Choose either of the following:
stone, paper, scissor
ENTER YOUR GUESS:paper
----------------------------------
YOU WIN!
----------------------------------
COMPUTER:stone
USER:paper
USER: 1
COMPUTER: 0
ENTER YOUR GUESS:scissor
----------------------------------
YOU WIN!
----------------------------------
COMPUTER:paper
USER:scissor
USER: 1
COMPUTER: 0
ENTER YOUR GUESS:paper
----------------------------------
YOU LOSE!
----------------------------------
COMPUTER:scissor
USER:paper
USER: 0
COMPUTER: 1
None
Process finished with exit code 0
我知道错误背后的原因(我正在def()
下使整个程序循环。结果,我得到了意外的结果)。我实际上是想从社区获得解决方案来解决我的问题。每一个建议都会受到赞赏。
谢谢
(使用的IDE:Pycharm)
答案 0 :(得分:0)
您必须在for循环上方使用comp = 0
和user = 0
,以便它们不会在每次迭代中重置。更好的做法是使用类,以便您可以更好地管理游戏
comp = 0 # computer's score
user = 0 # user's score
for i in range(3):
guess = ("stone", "paper", "scissor")# guess
user_g = input('ENTER YOUR GUESS:')
user_g.lower()
comp_g = choice(guess) #computer"s guess
答案 1 :(得分:0)
主要问题是在循环的每次迭代中将comp
和user
重置为0
。将这些行移到for
循环的开始上方。其他几点:
if/else
块之后continue
而不是break
return
执行任何操作,因此无需print
结果这是我的版本:
def stone_paper_scissor(rounds):
guess = ("stone", "paper", "scissors")
comp = 0
user = 0
print("Choose either of the following:")
print(*guess)
for i in range(rounds):
user_g = input('ENTER YOUR GUESS: ').lower()
if user_g not in guess:
print("invalid choice")
continue
comp_g = choice(guess)
if comp_g == user_g:
print( "IT'S A DRAW!" )
elif guess[guess.index(user_g) - 1] == comp_g:
print( "YOU WIN!" )
user += 1
else:
print( "YOU LOSE!" )
comp += 1
print("COMPUTER " + comp_g)
print("USER "+ user_g)
print("USER: %d" %(user))
print("COMPUTER: %d" %(comp))