I'm trying to write a rock-paper-scissors game in python.this is the code:
D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
play = False
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)
while play == False:
p_1 = input("which one?")
computer = D_0[randint(1, 3)]
print("my choice is: ",computer)
if p_1 == computer:
print("it's a draw")
elif p_1 == "scissors" and computer == "paper":
print("you won!")
elif p_1 == "paper" and computer == "scissors":
print("you lost!")
elif p_1 == "scissors" and computer == "rock":
print("you lost!")
elif p_1 == "rock" and computer == "paper":
print("you lost!")
elif p_1 == "rock" and computer == "scissors":
print("you won!")
elif p_1 == "paper" and computer == "rock":
print("you won!")
else:
print("Invalid input")
break
again = input("do you want another round?:")
if again == "yes":
play = False
else:
play = True
the program works well but I want it to ask the player whether or not he/she wants to do another round.If the answer is yes the program must restart the loop. The problem is I don't know how to do that.I know it's probably related to True and False and I attempted to do something as you can see in the code but it didn't work. please Help me.
A simple fix might be just putting your while loop as True
and continuing to loop until you break the execution:
D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)
while True:
p_1 = input("which one?")
computer = D_0[randint(1, 3)]
print("my choice is: ", computer)
if p_1 == computer:
print("it's a draw")
elif p_1 == "scissors" and computer == "paper":
print("you won!")
elif p_1 == "paper" and computer == "scissors":
print("you lost!")
elif p_1 == "scissors" and computer == "rock":
print("you lost!")
elif p_1 == "rock" and computer == "paper":
print("you lost!")
elif p_1 == "rock" and computer == "scissors":
print("you won!")
elif p_1 == "paper" and computer == "rock":
print("you won!")
else:
print("Invalid input")
again = input("do you want another round?:")
if again != "yes":
break
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.