请有人告诉我如何使用基本的评分系统来使用此代码。
import random
print 'Welcome to Rock, paper, scissors!'
firstto=raw_input('How many points do you want to play before it ends? ')
playerscore=+0
compscore=+0
while True:
choice = raw_input ('Press R for rock, press P for paper or press S for scissors, CAPITALS!!! ')
opponent = random.choice(['rock', 'paper' ,'scissors' ])
print 'Computer has chosen', opponent
if choice == 'R' and opponent == "rock":
print 'Tie'
playerscore = playerscore+0
compscore = compscore+0
elif choice == 'P' and opponent == "paper":
print 'Tie'
playerscore = playerscore+0
compscore = compscore+0
elif choice == 'S' and opponent == "scissors":
print 'Tie'
playerscore = playerscore+0
compscore = compscore+0
elif choice == 'R' and opponent == "paper":
print 'CPU Wins'
playerscore = playerscore+0
compscore = compscore+1
elif choice == 'P' and opponent == "scissors":
print 'CPU Wins'
playerscore = playerscore+0
compscore = compscore+1
elif choice == 'S' and opponent == "rock":
print 'CPU Wins'
playerscore = playerscore+0
compscore = compscore+1
elif choice == 'P' and opponent == "rock":
print 'You Win'
playerscore = playerscore+1
compscore = compscore+0
elif choice == 'S' and opponent == "paper":
print 'You Win'
playerscore = playerscore+1
compscore = compscore+0
elif choice == 'R' and opponent == "scissors":
print 'You Win'
playerscore = playerscore+1
compscore = compscore+0
print 'Player score is',playerscore
print 'Computer score is',compscore
if playerscore == firstto:
'You won the game :)'
exit()
elif compscore == firstto:
'You lost the game :('
exit()
答案 0 :(得分:3)
问题在于第3行的raw_input
。raw_input
总是返回一个字符串,而你需要的是一个int。如果您将第3行更改为:
firstto = int(raw_input('How many points do you want to play before it ends? '))
您的代码将有效。
要清理用户输入(以便在用户输入"hello"
而不是5
时代码不会崩溃),您可以将raw_input
调用包装成{{1 },try
声明。
例如:
except
顺便说一下,你的代码陷入无限循环,因为你在使用valid_input = False # flag to keep track of whether the user's input is valid.
while not valid_input:
firstto_str = raw_input('How many points do you want to play before it ends? ')
try:
# try converting user input to integer
firstto = int(firstto_str)
valid_input = True
except ValueError:
# user input that cannot be coerced to an int -> raises ValueError.
print "Invalid input, please enter an integer."
提供的字符串与整数进行比较时。这将始终返回False:
raw_input
答案 1 :(得分:1)
有很多方法可以优化此代码,但您的直接问题是raw_input
和点的输入。这会返回一个字符串,而您需要一个int
。用int()
包裹它你会没事的。也就是说,直到某人输入了无法解析的内容。
firstto = int(raw_input('How many points do you want to play before it ends? '))
编辑:如果您有兴趣,我会尝试优化您的代码(不要走极端):
import random
what_beats_what = [('R', 'S'), ('S', 'P'), ('P', 'R')]
choices = {'R': 'Rock', 'P': 'Paper', 'S': 'Scissors'}
def outcome(player_a, player_b):
for scenario in what_beats_what:
if player_a == scenario[0] and player_b == scenario[1]:
return 'A'
elif player_b == scenario[0] and player_a == scenario[1]:
return 'B'
print 'Welcome to Rock, paper, scissors!'
score_to_win = 0
while True:
try:
score_to_win = int(raw_input('How many points do you want to play before it ends? '))
if score_to_win > 0:
break
except ValueError:
pass
print 'Try again, with a positive integer.'
human_score = 0
cpu_score = 0
while human_score < score_to_win and cpu_score < score_to_win:
human_choice = ''
while True:
human_choice = raw_input('Press R for rock, press P for paper or press S for scissors: ').upper()
if human_choice in choices:
break
else:
print 'Try again ...'
cpu_choice = random.choice(choices.keys())
print 'Computer has chosen: {0}'.format(choices[cpu_choice])
result = outcome(human_choice, cpu_choice)
if result == 'A':
print "Human wins!"
human_score += 1
elif result == 'B':
print "CPU wins!"
cpu_score += 1
else:
print 'It is a tie!'
print 'Human score is: {0}'.format(human_score)
print 'CPU score is: {0}'.format(cpu_score)
print 'You won the game :)' if human_score > cpu_score else 'You lost the game :('
答案 2 :(得分:0)
修改您的第一个raw_input值。你在那里得到一个字符串。
为了获得更好的结果,还要验证输入: - )
while 1:
firstto=raw_input('How many points do you want to play before it ends? ')
if firstto.isdigit():
firstto = int(firstto)
break
else:
print "Invalid Input. Please try again"
这只接受带数字的字符串。即使有人将输入设为“5.0”,也会被忽略。
要更好地阅读raw_input,请点击here。要了解有关内置字符串方法的更多信息,请阅读here。
PS:这与问题无关。但有点建议。您的代码可以变得更简单。如果您正在学习,请将此代码保留为v0.1并根据您的进度进行更新。