Rock Paper Scissor Loop Issue Python



作为大多数学习python的人,我的任务是制作一个岩石剪刀游戏。到目前为止,我有一个代码,如果你只运行一次,它就可以工作。我的问题是,它需要循环运行,直到用户或计算机赢得三次。这就是我在没有设置循环的情况下所拥有的:

ug = input("Please enter your choice for: rock, paper, or scissors: ")
comp = [ ]
user = [ ]
random = np.random.randint(0, 3, 1)

# 1). Converts the randomly generated computer guess to str  
def guessC(random):
if random == 0:
return ("R")
if random == 1:
return ("S")
if random == 2:
return ("P")

compg = guessC(random)
# prints the user guess (ug) and comp guess (compg)
print("You guessed: ", ug)
print("The computer guessed: ", compg)   
#2). Determine winner      
def rockpaperscisccor(compg, ug):
if compg == "R": 
if ug == "R":
return 0,0
elif ug == "S":
return 1,0
elif ug == "P":
return 0,1
if compg == "P":
if ug == "P":
return 0,0
elif ug == "R":
return 1,0
elif ug == "S":
return 0,1
if compg == "S":
if ug == "S":
return 0,0
elif ug == "P":
return 1,0
elif ug == "R":
return 0,1    

cs,us = rockpaperscisccor(compg, ug)
# 3). take scores of game and append comp score to its own list and user score to 
# own list
def tallyuserH(us):
user = [ ]
user.append(us)
tus = 0
for i in user:
tus += i
return tus
sus = tallyuserH(us)
def compuserH(cs):
comp = [ ]
comp.append(cs)
tcs = 0
for i in comp:
tcs += i
return tcs
scs = compuserH(cs)

# 4). Score counter to determine score
def scorecounter(scs, sus):
if scs == 3:
print("The computer wins!", cs, "-", us, "!")
elif sus == 3:
print("You win!", us, "-", cs, "!")
elif scs > sus:
print("The computer leads!", cs, "-", us, "!")
elif sus > scs:
print("You lead!", us, "-", cs, "!")
elif sus == scs:
print("The score is tied at", cs, "-", us, "!")
else: 
print("That doesn't seem to be a valid input")

scorecounter(scs,sus)

当我把它放进一个while循环中时,这就是我迄今为止所得到的。当一个玩家达到3:时,它在我希望停止的地方无限地运行

print("Lets play rock, paper, scissor!")
def thegame():
i = 0
ug = input("Please enter your choice for: rock, paper, or scissors: ")
random = np.random.randint(0, 3, 1)
compg = guess(random)
print("You guessed: ", ug)
print("The computer guessed: ", compg)
cs,us = rockpaperscisccor(compg, ug)
sus = tallyuser(us)
scs = compuser(cs)
print ("user score is", sus)
print ("comp score is", scs)
while i < 6:
if scs == 3:
print("The computer wins!", cs, "-", us, "!")
elif sus == 3:
print("You win!", us, "-", cs, "!")
elif scs > sus:
print("The computer leads!", cs, "-", us, "!")
elif sus > scs:
print("You lead!", us, "-", cs, "!")
elif sus == scs:
print("The score is tied at", cs, "-", us, "!")
else: 
print("That doesnt seem to be a valid input")
i += 1
return i

def guess(random):
if random == 0:
return ("R")
if random == 1:
return ("S")
if random == 2:
return ("P")


def tallyuser(us):
user = [ ]
user.append(us)
tus = 0
for i in user:
tus += i
return tus
def compuser(cs):
comp = [ ]
comp.append(cs)
tcs = 0
for i in comp:
tcs += i
return tcs

thegame()

我不知道如何构造While循环。此外;分数计数器功能";需要保留自己的部分,这意味着我不能把这部分放在我决定获胜者的地方。如果这有道理的话!

谢谢你,

Rachel

尽我所能保留现有代码的精髓,不要对其进行太多更改:

import random
def get_round_points(comp_choice, user_choice):
if comp_choice == "R":
if user_choice == "R":
return 0, 0
elif user_choice == "S":
return 1, 0
elif user_choice == "P":
return 0, 1
if comp_choice == "P":
if user_choice == "P":
return 0, 0
elif user_choice == "R":
return 1, 0
elif user_choice == "S":
return 0, 1
if comp_choice == "S":
if user_choice == "S":
return 0, 0
elif user_choice == "P":
return 1, 0
elif user_choice == "R":
return 0, 1
def get_choice():
valid_choices = {'R', 'P', 'S'}
choice = ''
while choice not in valid_choices:
choice = input("Please enter your choice from (R)ock, (P)aper, or (S)cissors: ")
return choice
def score_counter(current_comp_score, current_user_score, win_threshold):
if current_comp_score == win_threshold:
print("The computer wins!", current_comp_score, "-", current_user_score, "!")
elif current_user_score == win_threshold:
print("You win!", current_user_score, "-", current_comp_score, "!")
elif current_comp_score > current_user_score:
print("The computer leads!", current_comp_score, "-", current_user_score, "!")
elif current_user_score > current_comp_score:
print("You lead!", current_user_score, "-", current_comp_score, "!")
elif current_user_score == current_comp_score:
print("The score is tied at", current_comp_score, "-", current_user_score, "!")
else:
print("That doesn't seem to be a valid input to score_counter...")
def play_rock_paper_scissors(win_threshold):
comp_score, user_score = 0, 0
while comp_score != win_threshold and user_score != win_threshold:
score_counter(comp_score, user_score, win_threshold)
user_choice = get_choice()
print("You guessed: ", user_choice)
comp_choice = ['R', 'P', 'S'][random.randint(0, 2)]
print("The computer guessed: ", comp_choice)
round_result = get_round_points(comp_choice, user_choice)
comp_score += round_result[0]
user_score += round_result[1]
score_counter(comp_score, user_score, win_threshold)
if __name__ == '__main__':
play_rock_paper_scissors(3)

示例用法:

The score is tied at 0 - 0 !
Please enter your choice from (R)ock, (P)aper, or (S)cissors: R
You guessed:  R
The computer guessed:  S
You lead! 1 - 0 !
Please enter your choice from (R)ock, (P)aper, or (S)cissors: P
You guessed:  P
The computer guessed:  R
You lead! 2 - 0 !
Please enter your choice from (R)ock, (P)aper, or (S)cissors: S
You guessed:  S
The computer guessed:  R
You lead! 2 - 1 !
Please enter your choice from (R)ock, (P)aper, or (S)cissors: S
You guessed:  S
The computer guessed:  P
You win! 3 - 1 !

最新更新