如何执行多个while循环?

时间:2015-10-23 23:26:44

标签: python loops while-loop

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

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))

2 个答案:

答案 0 :(得分:1)

在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

答案 1 :(得分:0)

试试这个

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()))