基本变量没有添加!!! (我的代码的一部分)

时间:2017-04-25 18:01:45

标签: python variables

number = 1

p1 = 0
p2 = 0

while number <5:
  gametype = input("Do You Want To Play 1 Player Or 2 Player")
  if gametype == "2":
    player1areyouready = input("Player 1, Are You Ready? (Yes Or No)") #Asking If Their Ready
  if player1areyouready == "yes": #If Answer is Yes
    print ("Great!")
  else: #If Answer Is Anything Else
    print ("Ok, Ready When You Are! :)")
  player2areyouready = input("Player 2, Are You Ready? (Yes Or No)") #Asking If Their Ready
  if player2areyouready == "yes":
   print ("Great, Your Both Ready!") #If Both Are Ready
  else:
    print ("Ok, Ready When You Are! :)")

  print ("Lets Get Right Into Round 1!") #Start Of Round 1
  game1 = input("Player 1, What Is Your Decision? (Rock, Paper or Scissors?) (No Capitals Please)") #Asking Player 1 For their Decision
  game2 = input("Player 2, What Is Your Decision? (Rock, Paper or Scissors?) (No Capitals Please)") #Asking Player 2 For Their Decision

  if game1 == game2:
   print("Tie!") 
   p1 + 0
   p2 + 0
   print ("Player 1's Score Currently Is...")    
   print (p1)

   print ("Player 2's Score Currently Is...") 
   print (p2) #I'm Programming A Rock, Paper Scissors Game

在这个Rock Paper剪刀游戏中,我发现了我的分数变量的麻烦。出于某种原因我编写代码的方式意味着我的分数不会加起来。请帮忙 :) 在此先感谢

3 个答案:

答案 0 :(得分:1)

如果是平局,则无需更新分数。但是:

if game1 != game2:
    p1 = p1 + score # Replace score with a number or assign score a value
    p2 = p2 + score

目前评分未更新,因为执行p1 + score不会更新p1的值。你需要重新分配它。请p1 = p1 + scorep1 += score

答案 1 :(得分:0)

当我在IDLE上运行你的代码时,我遇到了各种各样的问题。除此之外,如果您要做的只是添加一个变量及其所有数字,那么您可以执行

# Put this at the end of the if statement where player one is the winner.
p1 += 1 
# Put this at the end of the if statement where player two is the winner.
p2 += 1

所有这一切都是为变量中的当前数字加1。

应该那么简单。

答案 2 :(得分:0)

您还没有在分数中添加任何内容。首先,您处理分数的唯一陈述是表达式,而不是赋值。您需要通过将结果存储回变量来保留值,例如

number = number + 1

你可以缩短为

number += 1

其次,您已将0添加到p1和p2。即使您存储结果,该值也不会改变。

<强> REPAIR

您需要确定哪个玩家获胜,然后增加该玩家的分数。我不会为你写详细的代码,但请考虑一下:

if game1 == game2:
    print ("Tie!")
elif game1 == "Rock" and game2 == "Scissors":
    print ("Player 1 wins!")
    p1 += 1
elif game1 == "Rock" and game2 == "Paper":
    print ("Player 2 wins!")
    p2 += 1

print ("Player 1's Score Currently Is...", p1)    
print ("Player 2's Score Currently Is...", p2) 

你看到它是如何工作的吗?仅在玩家赢得一轮时更新分数。当然,你想要找到一种更通用的方式来挑选胜利者,而不是通过所有六种获胜组合,但这是对学生的锻炼。 : - )