【发布时间】:2015-03-20 02:45:18
【问题描述】:
我正在尝试为作业编写一个程序,您可以在其中输入特定命令,然后您可以对着计算机玩 Rock-Paper-Scissors-Lizard-Spock。 它已经完成并且一直在工作,直到我意识到分配说明希望我完成它,以便您继续玩游戏,直到一个人获得五场胜利。
所以我想,没什么大不了的,让我们加入一个 while 循环和一些变量来跟踪胜利。但是当我运行程序时,它只运行一次。我不知道我做错了什么 - 因为这应该有效。这是我第一次使用 Python(3.3 版)和这个 IDE,所以我真的需要一些帮助。通常我只是调试,但我不知道如何在这个 IDE 中工作。
这是我的代码。麻烦的while循环在底部。我几乎很肯定课堂上的一切都有效。我想指出我已经尝试过 while(computerWins
import random
computerWins = 0
userWins = 0
print ('SELECTION KEY:\nRock = r\nPaper = p\nScissors = sc\nLizard = l\nSpock = sp')
class rockPaperScissorsLizardSpock:
#Two methods for converting from strings to numbers
#convert name to number using if/elif/else
#also converts abbreviated versions of the name
def convertName(name):
if(name == 'rock' or name == 'r'):
return 0
elif(name == 'Spock' or name == 'sp'):
return 1
elif(name == 'paper' or name == 'p'):
return 2
elif(name == 'lizard' or name == 'l'):
return 3
elif(name == 'scissors' or name == 'sc'):
return 4
else:
print ('Error: Invalid name')
#convert number to a name using if/elif/else
def convertNum(number):
if(number == 0):
return 'rock'
elif(number == 1):
return 'Spock'
elif(number == 2):
return 'paper'
elif(number == 3):
return 'lizard'
elif(number == 4):
return 'scissors'
else:
print ('Error: Invalid number')
#User selects an option, and their selection is saved in the 'choice' variable
#Using a while loop so that the user cannot input something other than one of the legal options
prompt = True
while(prompt):
i = input('\nEnter your selection: ')
if(i=='r' or i=='p' or i=='sc' or i=='l' or i=='sp'):
prompt = False
else:
print('Invalid input.')
prompt = True
#Convert the user's selection first to a number and then to its full string
userNum = convertName(i)
userChoice = convertNum(userNum)
#Generate random guess for the computer's choice using random.randrange()
compNum = random.randrange(0, 4)
#Convert the computer's choice to a string
compChoice = convertNum(compNum)
print ('You chose', userChoice)
print ('The computer has chosen', compChoice)
#Determine the difference between the players' number selections
difference = (compNum - userNum) % 5
#Use 'difference' to determine who the winner of the round is
if(difference == 1 or difference == 2):
print ('The computer wins this round.')
computerWins = computerWins+1
elif (difference == 4 or difference == 3):
print ('You win this round!')
userWins = userWins+1
elif(difference == 0):
print ('This round ended up being a tie.')
#Plays the game until someone has won five times
while(computerWins != 5 and userWins != 5):
rockPaperScissorsLizardSpock()
if(computerWins == 5 and userWins != 5):
print ('The computer wins.')
elif(computerWins != 5 and userWins == 5):
print ('You win!')
【问题讨论】:
-
这么简单的东西,代码却很复杂。
-
class rockPaperScissorsLizardSpock:到底应该做什么。是的,如此简单的事情变得复杂 -
我应该让用户输入这些字母,并且使用数字来匹配这些值似乎是最直接的方法。如果这是 java 或者我不应该做这些特别的事情,它会更简单。您对这个 while 循环问题有什么建议吗?
-
我正在尽我所能。我是 python 新手。我以为它应该是一堂课。在这个循环之前它工作得很好。好吧,就像我说的那样,我确实在我的 while 循环中对这两个语句都使用了
-
对于这么简单的事情,不要为类定义而烦恼。只需创建一些您将调用的函数。 Python 被创建为像这样灵活,没有严格的类结构,因此您可以快速原型化简单的东西(您当然可以创建类,但这不是必需的)。另外,明确标记
__main__方法,它使代码更易于阅读。做类似if __name__ == "__main__":之类的事情,这是Java 的public static void main(String[] args)的Python 等价物
标签: python while-loop python-3.3