Reputation: 311
When I entered 4 as my maximum point limit, the code ends the loop at 5. I couldn't get it why
import random
p=0
x = int(raw_input("How many points are required for a win? "))
while p<=x:
y = raw_input("Choose (R)ock, (P)aper, or (s)cissors? ")
z1 = ('Rock', 'Paper', 'Scissors')
z = random.choice(z1)
if y=='r':
print "Human: Rock Computer: " + z
if z=='Rock':
print "A draw"
if z=='Paper':
print "Computer wins!"
if z=='Scissors':
print "Human wins!"
p +=1
if p ==(x-1):
print "Only need one more point!"
print "Your score is: " + str(p)
elif y=='p':
print "Human: Paper Computer: " + z
if z=='Paper':
print "A draw"
if z=='Rock':
print "Human wins!"
p +=1
if p==(x-1):
print "Only need one more point!"
print "Your score is: " + str(p)
if z=='Scissors':
print "Computer wins!"
elif y=='s':
print "Human: Scissors Coputer: " + z
if z=='Scissors':
print "A draw"
if z=='Paper':
print "Human wins!"
p +=1
if p==(x-1):
print "Only need one more point!"
print "Your score is: " + str(p)
if z=='Rock':
print "Computer wins!"
Output:
Welcome to Rock, Paper, Scissors!
How many points are required for a win? 4
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Paper
Computer wins!
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Rock
A draw
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Rock
A draw
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Scissors
Human wins!
Your score is: 1
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Paper
Computer wins!
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Scissors
Human wins!
Your score is: 2
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Scissors
Human wins!
Only need one more point!
Your score is: 3
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Scissors
Human wins!
Your score is: 4
Choose (R)ock, (P)aper, or (s)cissors? r
Human: Rock Computer: Scissors
Human wins!
Your score is: 5
Upvotes: 0
Views: 185
Reputation: 57490
Your while
loop condition is p<=x
, which means that if p
equals x
— i.e., if the human has exactly the required number of points — the loop will run again. Change it to while p<x:
.
Upvotes: 1