简体   繁体   中英

Using a while loop in a Rock, Paper, Scissors Type game. (Python)

I am trying to use a while loop so that this game will continue playing unless the user enters a string that isn't one of the choices in myList. Where would I place this while loop? (sorry I'm relatively new to coding).

import random
def AceDiamondSpade():

    #Set up a list which contains Ace, Diamond and Spade so that the computer can randomly choose one of them.
    myList = ["Ace", "Diamond", "Spade"]
    #
    choice = input("Ace, Diamond, or Spade? ")
    computerChoice = random.choice(myList)
    playAgain = True
    if str(choice) != str(myList[0]) and str(choice) != str(myList[1]) and str(choice) != str(myList[2]):
    playAgain = False 
    print ("invalid! Please enter again or make sure that you have capitalized the first letter of your answer!")
    if str(choice) == str(computerChoice):
        print ("You and the computer selected the same thing, you tie!") 
    if str(choice) == "Ace" and str(computerChoice) == "Diamond":
        print ("You selected Ace, and the comptuer selected Diamond, you win!")
    if str(choice) == "Diamond" and str(computerChoice) == "Spade":
        print ("You selected Diamond, and the Computer selected Spade, you win!")
    if str(choice) == "Spade" and str(computerChoice) == "Ace":
        print ("You selected Spade, and the comptuer selected Ace, you win!")
    if str(choice) == "Ace" and str(computerChoice) == "Spade":
        print ("You selected Ace, and the computer selected Spade, you lose!")
    if str(choice) == "Spade" and str(computerChoice) == "Diamond":
        print ("You selected Spade, and the computer selected Diamond, you lose!")
    if str(choice) == "Diamond" and str(computerChoice) == "Ace":
        print ("You selected Diamond, and the computer selected Ace, you lose!")
import random


def newfunc(string1, string2):
    if string1 == string2:
        print "You selected",string1,"and the computer selected",string2,"you win!"
    else:
        print "You selected",string1,"and the computer selected",string2,"you lose!"

def AceDiamondSpade():
    #Set up a list which contains Ace, Diamond and Spade so that the computer can randomly choose one of them.
    myList = ["Ace", "Diamond", "Spade"]
    #
    while(True):
        choice = raw_input("Ace, Diamond, or Spade? ")
        computerChoice = random.choice(myList)
        if choice not in myList:
            print "That was not a valid choice."
            break
        newfunc(choice,computerChoice)

AceDiamondSpade()

This is based on diegoaguilar's answer, but it adds in the prospect of a new function.

I'm sure you just copy/pasted your code straight onto the site, but be careful with your indentation and if possible, give some spacing in between your lines, it makes things much easier to read.

You can use a sort of do while which in python is structured in a few different ways :

So resumed you're code should look like this:

def AceDiamondSpade():

    #Set up a list which contains Ace, Diamond and Spade so that the computer can randomly choose one of them.
    myList = ["Ace", "Diamond", "Spade"]

    while True:
        #Execute your game logic here
        if input("Ace, Diamond, or Spade? ") not in myList:
            break;

At while True you're assuming the code block below (the game) will keep executing infinitely. However at if condition you check whether the result input is not member of the list referred the while loop will end.

And I agree with the comment, you don't need to use str at any point of your code. Going for a few more functions would make it look nicer too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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