【问题标题】:Rock Paper Scissor Loop Issue Python石头剪刀布循环问题 Python
【发布时间】:2020-11-14 10:28:47
【问题描述】:

作为大多数学习 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 循环。此外,“计分功能”需要保留自己的部分,这意味着我不能将那部分嵌套在我确定获胜者的地方。如果这有意义!

谢谢,

雷切尔

【问题讨论】:

  • 我正在初始化的变量在哪里?它在循环中也不会改变,这就是为什么它是无限的。
  • @luthervespers 我应该是圆的!抱歉,我不知道如何让它每次都计数。

标签: python while-loop


【解决方案1】:

尽我所能保留现有代码的精髓,而不是过多地改变它:

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 !

【讨论】:

  • Shash Sinha 你能向我解释一下获取用户输入的循环吗?为什么你不能在那里使用输入?
  • 这将验证用户除了RPS 之外没有放置任何内容。 IE。在他们做出有效的猜测之前,他们无法做出猜测。希望这是有道理的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多