繁体   English   中英

如何执行多个while循环?

[英]How to execute multiple while loops?

我想知道是否有人可以告诉我如何让计算机在每一轮之后选择新的选择。 我将代码的下半部分覆盖了所有选择,但是事实证明,我的代码在每次计算机使用相同选择的地方运行。 有没有一种方法可以设置我的代码,以便计算机从列表中选择新的内容。 谢谢!

import random

def computerChoice():
    gameList = ["Rock", "Paper", "Scissors"]
    computerChoice = random.choice(gameList)
    print(computerChoice)




def userChoice(computerChoice):
    userScore = 0
    computerScore = 0
    rounds = 0
    print("Welcome to Rock, Paper, Scissors. You are playing against the computer on best out of three game, winner takes all! Have Fun!")
    while userScore < 2 and computerScore < 2:
        userAnswer = input("Please choose Rock, Paper, or Scissors: ")
        if userAnswer == "Rock" and computerChoice != "Paper":
            userScore += 1
            rounds += 1
            print("The Computer Loses and You Win the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Paper" and computerChoice != "Scissors":
            userScore += 1
            rounds += 1
            print("The Computer Loses and You Win the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Scissors" and computerChoice != "Rock":
            userScore += 1
            rounds += 1
            print("The Computer Loses and You Win the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Rock" and computerChoice == "Paper":
            computerScore += 1
            rounds += 1
            print("The Computer Wins and You Lose the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Paper" and computerChoice == "Scissors":
            computerScore += 1
            rounds += 1
            print("The Computer Wins and You Lose the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Scissors" and computerChoice == "Rock":
            computerScore += 1
            rounds += 1
            print("The Computer Wins and You Lose the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Rock" and computerChoice == "Rock":
            print("This round is a PUSH!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Scissors" and computerChoice == "Scissors":
            print("This round is a PUSH!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Paper" and computerChoice == "Paper":
            print("This round is a PUSH!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        else:
            print("Whatever you just inputed doesn't work noodlehead, try again!")
            continue

x = computerChoice()
print(userChoice(x))

将其移动到while循环中:

computerChoice = random.choice(gameList)

当前,您正在保存一个选择,然后每次都使用它。 每次都会在哪里创建新选择:

while userScore < 2 and computerScore < 2:
    userAnswer = input("Please choose Rock, Paper, or Scissors: ")
    computerChoice = random.choice(gameList)
    # Compare to see who won.

请注意, gameList必须在该范围内可用,因此您必须将其作为函数参数传递或将其包含在函数中。 这确实改变了函数的性质:

def game():
    print("Welcome to Rock, Paper, Scissors. You are playing against the computer on best out of three game, winner takes all! Have Fun!")
    userScore, computerScore, rounds = game_loop()

def game_loop(available_options: list=["Rock", "Paper", "Scissors"]):
    computerScore = 0
    userScore = 0
    rounds = 0
    while userScore < 2 and computerScore < 2:
        userAnswer = input("Please choose Rock, Paper, or Scissors: ")
        computerChoice = random.choice(available_options)
        // compare and score (preferably in their own functions)

    return userScore, computerScore, rounds

尝试这个

import random

def computerChoice():
    gameList = ["Rock", "Paper", "Scissors"]
    return random.choice(gameList)

def userChoice(computerChoice):
    userScore = 0
    computerScore = 0
    rounds = 0
    print("Welcome to Rock, Paper, Scissors. You are playing against the computer on best out of three game, winner takes all! Have Fun!")
    while userScore < 2 and computerScore < 2:
        userAnswer = input("Please choose Rock, Paper, or Scissors: ")
        #all your if statements and stuff
        computerChoice = computerChoice()

print(userChoice(computerChoice()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM