我目前在代码中遇到一个问题,我正在尝试创建一个剪刀石头布游戏。我的计数方法似乎不起作用(当程序循环执行时,输赢计数器都为0),所以我希望可以得到一些帮助。我对Python和堆栈溢出相当陌生,因此请记住这一点。很抱歉,代码太长了:
import random
import time
win=0
loss=0
def compare():
if choice == "rock" and opp == "paper":
print("You lose!")
loss+1
elif choice == "paper" and opp == "scissors":
print("You lose!")
loss+1
elif choice== "scissors" and opp == "rock":
print("you lose")
loss+1
elif choice == "scissors" and opp == "paper":
print("you win")
win+1
elif choice == "rock" and opp =="scissors":
print("you win")
win+1
elif choice == "paper" and opp == "rock":
print("you win!")
win+1
else:
print("...")
op=["rock","paper","scissors"]
phrase=["Rock paper scissors shoot!","janken bo!", "pierre-feuille-ciseaux!","papier, kamień, nożyczki!"]
while True:
print ("You have",win,"wins and",loss,"losses")
choice=input(random.choice(phrase)).lower()
if choice not in op:
print("Rock paper or scissors")
continue
else:
print()
opp=random.choice(op)
print (opp)
compare()
while opp==choice:
choice=input(random.choice(phrase)).lower()
time.sleep(1)
opp=random.choice(op)
print (opp)
compare()
感谢您的帮助。谢谢
答案 0 :(得分:2)
每次编写variable + 1
时,都不会更改变量。
例如:
def compare():
if choice == "rock" and opp == "paper":
print("You lose!")
loss+1
loss+1
不执行任何操作,即未分配值。
您需要将值分配给变量。例如:loss += 1
答案 1 :(得分:1)
欢迎使用StackOverflow!
在您的if statements
中,您实际上并没有增加计数。您需要执行win = win + 1
和loss = loss + 1
def compare():
if choice == "rock" and opp == "paper":
print("You lose!")
loss = loss + 1
现在,您还可以执行一些“简写”语法。 loss += 1
等同于loss = loss + 1
。