import random
print("=====Rock Paper Scissors: The Python Game=====")
print("\n")
name = input("Player please enter you name:\n")
print("\n")
myItem = input("Enter Either Rock Paper or Scissors:\n")
if myItem != "Rock" or myItem != "Paper" or myItem != "Scissors":
print("Are you Ok!! I said enter rock paper or scissors")
#Computer's Choices
computerItem = random.randint(1,3)
if computerItem == 1:
computerItem = "Rock"
print(f"Computer chooses {computerItem}...")
if computerItem == 2:
computerItem = "Paper"
print(f"Computer chooses {computerItem}...")
if computerItem == 3:
computerItem = "Scissors"
print(f"Computer chooses {computerItem}...")
if myItem == computerItem:
print("Try Again!! You and the CPU picked the same thing")
#How I can loose to the CPU
if myItem == "Rock" and computerItem == "Paper":
print("[Paper covers Rock]")
print("CPU WINS!!!")
if myItem == "Paper" and computerItem == "Rock":
print("[Paper covers Rock]")
print(f"{name} WINS!!!")
if myItem == "Scissors" and computerItem == "Rock":
print("[Rock Smashes Scissors]")
print("CPU WINS!!!")
if myItem == "Rock" and computerItem == "Scissors":
print("[Rock smashes Scissors]")
print(f"{name} WINS!!!")
if myItem == "Paper" and computerItem == "Scissors":
print("[Scissors cut Paper]")
print("CPU WINS!!!")
if myItem == "Scissors" and computerItem == "Paper":
print("[Scissors cut Paper]")
print(f"{name} WINS!!!")
I have been trying to make this rock paper scissors game loop all day. But each time I put the code in a while loop it keeps giving errors.
So how do I loop through the game without any issues?
PS - I just started learning python
CodePudding user response:
I have tested your code and it works well, as for the looping, all I did was insert the whole program code into an infinite while loop while True
, which is a loop that never stops until it's told to, then after each turn, I ask the user if he wants to play again or not, if he doesn't want to play again, the program breaks out of the infinite while loop and exits.
Find the code below:
import random
while True:
print("=====Rock Paper Scissors: The Python Game=====")
print("\n")
name = input("Player please enter you name:\n")
print("\n")
myItem = input("Enter Either Rock Paper or Scissors:\n")
if myItem != "Rock" or myItem != "Paper" or myItem != "Scissors":
print("Are you Ok!! I said enter rock paper or scissors")
#Computer's Choices
computerItem = random.randint(1,3)
if computerItem == 1:
computerItem = "Rock"
print(f"Computer chooses {computerItem}...")
if computerItem == 2:
computerItem = "Paper"
print(f"Computer chooses {computerItem}...")
if computerItem == 3:
computerItem = "Scissors"
print(f"Computer chooses {computerItem}...")
if myItem == computerItem:
print("Try Again!! You and the CPU picked the same thing")
#How I can loose to the CPU
if myItem == "Rock" and computerItem == "Paper":
print("[Paper covers Rock]")
print("CPU WINS!!!")
if myItem == "Paper" and computerItem == "Rock":
print("[Paper covers Rock]")
print(f"{name} WINS!!!")
if myItem == "Scissors" and computerItem == "Rock":
print("[Rock Smashes Scissors]")
print("CPU WINS!!!")
if myItem == "Rock" and computerItem == "Scissors":
print("[Rock smashes Scissors]")
print(f"{name} WINS!!!")
if myItem == "Paper" and computerItem == "Scissors":
print("[Scissors cut Paper]")
print("CPU WINS!!!")
if myItem == "Scissors" and computerItem == "Paper":
print("[Scissors cut Paper]")
print(f"{name} WINS!!!")
exit = input("\nWould you like to play game? Y/N")
if exit=="N":
break
If you have any more questions, I'd love to answer them, good luck!
CodePudding user response:
import random
sl = 0 #记录人赢了多少次
sb = 0 #记录机器人赢了多少次
pj = 0 #记录平局多少次
#创建一个列表来存储数字对应的剪刀石头布
li=["石头","剪刀","布"]
dict={1:"剪刀",2:"石头",3:"布"}
with open(file='logs.txt',mode='a',encoding='utf-8') as f:
print("----------石头剪刀布游戏开始----------")
print("请按下面提示出拳")
print("石头【1】,剪刀【2】,布【3】,结束游戏【4】")
#循环开始
while True:
try:
ren_number=int(input("请输入你的选项:"))
except:
print("您的输入有误,请输入正确的数")
continue
else:
#创建一个机器人的随机数
reboot_number= int(random.randint(1, 3))
#如果用户输入的数为1至3的话,就进行判断
if 1 <= ren_number <= 3:
#寻找规律,用户赢的情况只有两种:用户输入减去机器人输入结果为-1或者是为2
if (ren_number - reboot_number == -1) or (ren_number - reboot_number == 2):
print("您出的是:{},电脑出的是:{},您胜利了".format(dict[ren_number],dict[reboot_number])) #直接字典取值
f.write("您出的是:{},电脑出的是:{},您胜利了".format(dict[ren_number],dict[reboot_number]))
sl =1 #用来统计赢的次数
#平局情况就是用户输入等于电脑随机输入
elif ren_number == reboot_number:
print("您出的是:{},电脑出的是:{},结果为平局".format(li[ren_number-1],li[reboot_number-1])) #列表取值,得减去1
f.write("您出的是:{},电脑出的是:{},结果为平局".format(li[ren_number-1],li[reboot_number-1]))
pj =1 #用来统计平局的次数
#用户赢了和平局都有了,那么剩下的情况就是用户输了,就不用判断了
else:
print("您出的是:{},电脑出的是:{},很遗憾您输了".format(li[ren_number-1],li[reboot_number-1])) #列表取值,得减去1
f.write("您出的是:{},电脑出的是:{},很遗憾您输了".format(li[ren_number-1],li[reboot_number-1]))
sb =1 #用来统计输的次数
#用户输入为4的话程序退出,汇报战况
elif ren_number == 4:
print("游戏结束,赛况为:平局{}次,你赢了{}次,机器人赢了{}次".format(pj, sl, sb))
break
#如果输入的为1至4以外的,提示输入有误,重新输入
else:
print("您输入的有误,请重新输入!")