我在编写Rock,Paper,Scissors for Python时遇到了问题。遇到平局时我的问题就出现了。当有一个平局时,我的程序应该在领带的if语句中进入while循环并再次向玩家重新提出相同的问题,摇滚,纸张或剪刀,直到它突破领带。我已将链接附加到问题的图像:
在第5轮:你可以看到问题。
我正在参加编程课程,所以我还是初学者,我不知道我做错了什么。
import random
def rock_paper_scissors():
playerscore = 0
computerscore = 0
rounds = input('\nHow many points does it take to win?: ')
count = 1
while playerscore or computerscore != int(rounds):
print('\n********************* ROUND #',count,'*********************')
player = input('\nPick your throw: [r]ock, [p]aper, or [s]cissors?: ')
computerthrow = random.randint(0,2)
if (computerthrow == 0):
computer = "rock"
computer = 'r'
elif (computerthrow == 1):
computer = "paper"
computer = 'p'
elif (computerthrow == 2):
computer = "scissors"
computer = 's'
if (player == computer):
print('Tie!')
while (player == computer):
player = input('\nPick your throw: [r]ock, [p]aper, or [s]cissors?: ')
computerthrow = random.randint(0,2)
if (computerthrow == 0):
computer = "rock"
computer = 'r'
elif (computerthrow == 1):
computer = "paper"
computer = 'p'
elif (computerthrow == 2):
computer = "scissors"
computer = 's'
print(computer)
elif (player == 'r'):
if (computer == "p"):
print('Computer threw paper, you lose!')
computerscore=computerscore+1
else:
print('Computer threw scissors, you win!')
playerscore = playerscore+1
#count = count + 1
elif (player == 'p'):
if (computer == "r"):
print('Computer threw rock, you win!')
playerscore = playerscore+1
else:
print('Computer threw scissors, you lose!')
computerscore=computerscore+1
#count = count + 1
elif (player == 's'):
if (computer == "p"):
print('Computer threw paper, you win!')
playerscore = playerscore+1
else:
print('Computer threw rock, you lose!')
computerscore=computerscore+1
count = count + 1
print('\nYour score: ',playerscore)
print('Computer''s score: ',computerscore,'\n')
print('********************* GAME OVER ********************')
def main():
print('ROCK PAPER SCISSORS in Python')
print()
print('Rules: 1) Rock wins over Scissors.')
print(' 2) Scissors wins over Paper.')
print(' 3) Paper wins over Rock.')
rock_paper_scissors()
main()
答案 0 :(得分:1)
您的问题来自您构建控制语句的方式(if,elif,else)。当你在循环中输入你的领带时,你会不断地运行它直到有人获胜并且看起来它没有问题。问题是,一旦你这样做,python解释器会跳过该控制块中的所有elif和else语句(如果我说如果x == 3:执行其他操作:那样做)我不希望python通过如果x确实== 3,则使用else条件。对不起,如果令人困惑,长话短说,你需要确保即使你的领带块被执行,你仍然继续得分并开始一个新的。这样做的简单方法就是将elif(player ==" r")更改为if语句。这样,解释器将得分控制序列视为自己的块,并且一旦你分配了每个玩家所做的投掷,它就会一直执行。
def f(x):
if (x == 0):
print("1")
x += 1
elif (x == 1):
print("2")
print("Done!")
def g(x):
if (x == 0):
print("1")
x += 1
if (x == 1):
print("2")
print("Done!")
如果你打电话给f(0): Python将打印出1然后完成!
如果你打电话给g(0): Python将打印出1然后2然后完成!
答案 1 :(得分:0)
您当前的问题是,您在与其他评分的处理相同的if/elif
链中拥有平局的处理代码。
你说的是:
if tied
stuff
elif player == r
...
结果,当比赛结束时,正确循环,直到没有平局。但是跳过更新分数的代码,因为这是if / elif / else的工作方式!
立即修复将破坏if / elif链,并将tie-handling放入单独的if块中:
if tied:
loop until not tied
# now do scoring
if player == 'r':
... etc
话虽如此,我还要补充几点:
您按顺序为同一个变量分配两个值:
computer = 'rock'
computer = 'r'
这没有做任何事情,因为第二个覆盖了第一个。你应该删除每对中的第一行。
您应该编写一个重复要求用户选择一个功能的功能,直到它获得摇滚,纸张或剪刀。使用while循环。
random
module中有一个函数可以从列表,元组或字符串中选择一个项目并将其返回。您可以使用它来使计算机直接选择r,p,s。
您可以通过提前确定结果来简化代码。假设您先拥有计算机选择权。然后,如果计算机选择'r',你知道'p'是胜利而's'是输。将赢/输选择存储在一对变量中,所有测试都可以归结为一个测试:
computer = computer_pick()
if computer == 'r':
winner = 'p'
computer = 'rock'
elif computer == 'p':
...
player = player_pick()
if tied ...
if player == winner:
print("Computer picked", computer, "- you win!")
else:
...