mercredi 6 septembre 2023

how do i create a game of rock, paper and scissors where the game doesn't end until the user says no when asked if he wants to play again?

I am writing this game of rock, paper and scissors. I have used a while loop and some conditions. in one of the conditions, I have added that if the user wins, don't end the game right away, but instead ask if he wants to play again. if the user says no, then we break out of the loop. but if the user says yes, we start from the beginning of the loop again. but since I want to keep asking this question, how long do I continue this code? do I use some loop again, and if so, then how?

This is the code i have written:

     import random
    a=['rock','paper','scissors']
    b=random.choice(a)
    while True:
        c=input(print("rock,paper,or scissors? "))
        if (b=='rock' and c=='rock') or (b=='paper' and c=='paper') or (b=='scissors' and c=='scissors'):
            print(f"computer's choice:{b}\nyou are tied with the computer")
        elif(b=='rock' and c=='scissors') or (b=='scissors' and c=='paper') or (b=='paper' and c=='rock'):
            print (f"computer's choice:{b}\nyou have unfortunately lost to the computer. do not give up though and try again.")
        elif(b=='scissors' and c=='rock') or (b=='paper' and c=='scissors') or (b=='rock' and c=='paper'):
            print(f"computer's choice:{b}\ncongratulations! you have won against the computer!!")
            d=input(print("giving your answer in yes or no,would you like to play again?"))
            if d=='no':
                break
            else:
                for i in range(1000):
                    c = input(print("rock,paper,or scissors? "))
                    if (b == 'rock' and c == 'rock') or (b == 'paper' and c == 'paper') or (
                            b == 'scissors' and c == 'scissors'):
                        print(f"computer's choice:{b}\nyou are tied with the computer")
                    elif (b == 'rock' and c == 'scissors') or (b == 'scissors' and c == 'paper') or (
                            b == 'paper' and c == 'rock'):
                        print(
                            f"computer's choice:{b}\nyou have unfortunately lost to the computer. do not give up though and try again.")
                    elif (b=='scissors' and c=='rock') or (b=='paper' and c=='scissors') or (b=='rock' and c=='paper'):
                        print(f"computer's choice:{b}\ncongratulations! you have won against the computer!!")
                        d = input(print("giving your answer in yes or no,would you like to play again?"))
                        if d=='no':
                            break
                        else:
                            for i in range(1000):
                                c = input(print("rock,paper,or scissors? "))
                                if (b == 'rock' and c == 'rock') or (b == 'paper' and c == 'paper') or (
                                        b == 'scissors' and c == 'scissors'):
                                    print(f"computer's choice:{b}\nyou are tied with the computer")
                                elif (b == 'rock' and c == 'scissors') or (b == 'scissors' and c == 'paper') or (
                                        b == 'paper' and c == 'rock'):
                                    print(
                                        f"computer's choice:{b}\nyou have unfortunately lost to the computer. do not give up though and try again.")
                                elif (b=='scissors' and c=='rock') or (b=='paper' and c=='scissors') or (b=='rock' and c=='paper'):
                                    print(f"computer's choice:{b}\ncongratulations! you have won against the computer!!")

I don't know where I am going with this.




Aucun commentaire:

Enregistrer un commentaire