因此,我试图制作一个有多轮的摇滚,纸张,剪刀游戏,并计算其内部的赢/输/数量,我认为如果它失败了,我认为最多,但是我'我在计算循环中的损失/获胜/关系数量方面遇到了麻烦。
我认为我应该做的是包括"返回"这里的声明,但我不确定如何处理它,我已经尝试查看其他示例,我仍然不确定它是如何工作的所以我现在在代码中的回报只显示在哪里我认为应该是。
我觉得我在这些方面也应该有+1,但是我再也不知道如何处理这个问题。
我删除了 my_choice ==剪刀和 my_choice == paper 因为我认为整个胜利/关系/亏损业务的运作方式与 my_choice == rock
import random
rounds = eval(input("Enter the number of rounds: "))
comp_pack = ["rock", "paper", "scissors"]
my_pack = ["rock", "paper", "scissors"]
for i in range (rounds):
count_win = ""
count_losses = ""
count_ties = ""
my_choice = input("Enter rock, paper, or sissors: ")
comp_choice = comp_pack[random.randint(0, len(comp_pack)-1)]
if my_choice == comp_choice:
print("tie")
return count_ties
elif my_choice == "rock":
if comp_choice == "paper":
print("paper covers rock loser")
return count_losses
elif comp_choice == "scissors":
print("rock smashes scissors winner")
return count_wins
print(count_losses)
print(count_wins)
print(count_ties)
答案 0 :(得分:2)
将你的计数器变量拉出for循环,并根据获胜/失败/平局使用+= 1
进行适当增量:
import random
rounds = eval(input("Enter the number of rounds: "))
comp_pack = ["rock", "paper", "scissors"]
my_pack = ["rock", "paper", "scissors"]
count_wins = 0
count_losses = 0
count_ties = 0
for i in range (rounds):
my_choice = input("Enter rock, paper, or sissors: ")
comp_choice = comp_pack[random.randint(0, len(comp_pack)-1)]
if my_choice == comp_choice:
print("tie")
count_ties += 1
elif my_choice == "rock":
if comp_choice == "paper":
print("paper covers rock loser")
count_losses += 1
elif comp_choice == "scissors":
print("rock smashes scissors winner")
count_wins +=1
print(count_losses)
print(count_wins)
print(count_ties)
答案 1 :(得分:0)
return
语句仅用于函数。
所以,如果你想使用' return'声明,你必须发挥作用。
我希望我的代码可以帮到你。
import random
def game(round):
count_wins = 0
count_losses = 0
count_ties = 0
for i in range(round):
# I just defined r, c, p in number.
pack = {"scissors": 1, "rock": 2, "paper": 3}
my_choice = pack[input("Enter rock, paper, or scissors: ")]
comp_choice = random.randint(1, len(pack))
if my_choice == comp_choice:
print("tie")
count_ties += 1
elif comp_choice - my_choice == 1 or comp_choice - my_choice == -1:
if comp_choice < my_choice:
# I defined r, c, p in number because of this compare algorithm. I will explain about this at bottom. And I omitted some messages like "paper covers rock", if you want, you can define message dictionary and print here.
print("win")
count_wins += 1
else:
print("lose")
count_losses += 1
else:
if comp_choice < my_choice:
print("lose")
count_losses += 1
else:
print("win")
count_wins += 1
return count_wins, count_losses, count_ties
if __name__ == "__main__":
# You can use function game like this.
game_result = game(3)
print("Win: %d" % game_result[0])
print("Lose: %d" % game_result[1])
print("Tie: %d" % game_result[2])
这是我在我的代码中使用的算法的详细解释。
剪刀:1,摇滚:2,纸张:3如果数字相同=&gt; TIE
如果| comp - my |是1: 大数胜= = Rock(2)赢得Scissors(1),Paper(3)赢得Rock(2)
如果| comp - my |是2: 大数丢失=&gt;剪刀(1)赢得论文(3)
答案 2 :(得分:0)
复制/粘贴的间距可能很奇怪,但这基本上是最终的结果:
import random
def winner(comp_pack):
my_choice = input("Enter rock, paper, or scissors: ")
comp_choice = comp_pack[random.randint(0, len(comp_pack)-1)]
if my_choice == comp_choice:
print("tie")
return "tie"
elif my_choice == "rock":
if comp_choice == "paper":
print("paper covers rock, computer wins!")
return "comp"
elif comp_choice == "scissors":
print("rock smashes, scissors winner")
return "user"
elif my_choice == "paper":
if comp_choice == "scissors":
print("scissors cut paper, computer wins!")
return "comp"
elif comp_choice == "rock":
print("paper covers rock, you win!")
return "user"
elif my_choice == "scissors":
if comp_choice == "paper":
print("scissors cut paper, you win!")
return "user"
elif comp_choice == "rock":
print("rock smashes paper, computer wins!")
return "comp"
def main():
rounds = eval(input("Enter the number of rounds: "))
comp_pack = ["rock", "paper", "scissors"]
i_win = 0
comp_win = 0
tie = 0
for i in range (rounds):
result = winner(comp_pack)
if result == "user":
i_win += 1
elif result == "comp":
comp_win += 1
elif result == "tie":
tie += 1
print ("current score: user ", i_win , ", computer ", comp_win)
if i_win > comp_win:
print("You win!")
elif i_win < comp_win:
print("You lose!")
else:
print("Tie!")
main()