Python-我如何让石头剪刀运行多个回合

  • 本文关键字:运行 石头 Python- python
  • 更新时间 :
  • 英文 :


编辑:感谢您的帮助!

所以我在初级班,没有太多经验,我遇到了这个问题:我正在努力让游戏重复五次,我想发生的是

玩家输入选择

游戏吐出结果

进入第2轮

再次请求选择

游戏吐出结果。。。etc

实际发生的是

玩家输入选择

游戏吐出结果

进入第2轮

程序要求选择(我认为)

在第1轮之后,if/elif被完全忽略,因为无论我输入什么,都没有结果

在程序结束之前可以点击回车键五次

我在做这件事时面临的挑战是尽可能简洁,因为我可以做到,但最终的程序会非常长,非常难看。

无论如何,要问一个具体的问题,是否有可能像这样重复使用同一个变量五次?为什么在第1轮之后if/else被完全忽略?

import random
cpu_choice = ["rock","paper","scissors","dynamite"]
for i in range(5):
choice = input().lower().strip("!.?")
cpu_choice = random.choice(cpu_choice)
if "rock" in choice:
if cpu_choice == "rock":
print("It's a tie!")
elif cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Haha! I won!")
elif "paper" in choice:
if cpu_choice == "rock":
print("Dang, I lost!")
elif cpu_choice == "paper":
print("It's a tie!")
elif cpu_choice == "scissors" or cpu_choice == "dynamite":
print("Haha! I won!")
elif "scissors" in choice:
if cpu_choice == "rock":
print("Haha! I won!")
elif cpu_choice == "scissors":
print("It's a tie!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Dang, I lost!")
elif "dynamite" in choice:
if cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "dynamite":
print("It's a tie!")
elif cpu_choice == "paper" or cpu_choice == "rock":
print("Haha! I won!")
else:
print("Please use rock, paper, scissors, or dynamite!")

```

我最初发布了一个答案。几分钟后,有4个答案,都说了同样的话。我曾想过删除我的答案或直接留下,但我认为最好通过提供增强的答案来为提问者和社区提供更多的东西。

正如其他答案所指出的,您的问题是您将cpu_choice变量用于两个目的,这破坏了程序的逻辑。你可以从其他答案中获得更多细节。

当你成为一名更好的程序员时,你会学到一件事,那就是识别出你什么时候有可以消除的重复。我们称之为DRY;不要重复自己"道德原则这是代码的一个明显特征。。。有四个代码块几乎相同,唯一的区别是一些字符串常量的特定值。

在这样的情况下,您将了解到您想要参数化重复的代码,这样您只需要该代码的一个副本。这不仅为您节省了屏幕上的时间和空间,而且还使代码更易于维护。如果你想改变处理用户输入的值与计算机选择的值的比较的逻辑,你必须在四个地方进行更改。如果您将代码缩减为一个块,那么您只需要进行一次更改。

以下是您的代码版本,它根据DRY原则将四个代码块减少为一个。它还有一些其他增强功能。它会提示用户,让他们知道要输入什么。它将根据用户的意愿进行任意回合的游戏,用户可以在不输入任何字母的情况下按Return退出游戏:

import random
# Valid choices, each with a list of what that choice can beat
choices = {"rock":["scissors"],"paper":["rock"],"scissors":["paper" "dynamite"],"dynamite":["scissors"]}
while True:
print("rock, paper, scissors or dynamite? >", end="")
user_choice = input().lower().strip("!.?")
cpu_choice = random.choice(list(choices.keys()))
if not user_choice:
# End the program on an empty input
break
elif user_choice not in choices:
# Complain about an invalid choice
print("That's not a valid input value!")
else:
# Show what the computer picked
print("I picked " + cpu_choice)
# Do the appropriate thing based on the two choices
if user_choice == cpu_choice:
# The two choices were the same
print("It's a tie!")
elif cpu_choice in choices[user_choice]:
# The computer's choice is not in the list of things that the user's choice beats
print("Haha! I won!")
else:
# The computer's choice is in the list of things that the user's choice beats
# beats, so it must have won
print("Dang, I lost!")

以下是示例运行的样子:

rock, paper, scissors or dynamite? >rock
I picked paper
Dang, I lost!
rock, paper, scissors or dynamite? >rock
I picked scissors
Haha! I won!
rock, paper, scissors or dynamite? >paper
I picked paper
It's a tie!
rock, paper, scissors or dynamite? >blah
That's not a valid input value!
rock, paper, scissors or dynamite? >rock
I picked dynamite
Dang, I lost!
rock, paper, scissors or dynamite? >

我希望这能通过教你DRY原理来帮助你!

代码的问题是这一行:cpu_choice = random.choice(cpu_choice)。您从列表中选择一个对象,然后将其保存在cpu_choice本身中。

换句话说,在第一轮之后,您无法访问cpu_choice本身,因为您在上一轮覆盖了它。诀窍很简单;更改变量名称:

import random
cpu_choice2 = ["rock","paper","scissors","dynamite"]
for i in range(5):
choice = input().lower().strip("!.?")
cpu_choice = random.choice(cpu_choice2)
if "rock" in choice:
if cpu_choice == "rock":
print("It's a tie!")
elif cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Haha! I won!")

我刚刚在您的cpu_list名称中添加了一个2

您使用cpu_choice做两件不同的事情。第一次,它列出了cpu的潜在选择。但随后您将其重新定义为cpu实际选择的结果。这意味着(比如说cpu在第一次迭代中选择了rock),在第二次迭代中,你有"cpuchoice=random.cochoice("rock")",因为python将字符串视为列表,所以它将输出";r〃"o"c";或";k";。您不会收到错误消息,因为您的内部if语句中没有else语句。

只要将第二行重命名为potential_choices就可以了。

将cpu_choice替换为cpu_ch

import random
cpu_ch = ["rock","paper","scissors","dynamite"]
for i in range(5):
choice = input().lower().strip("!.?")
cpu_choice = random.choice(cpu_ch)
if choice == "rock":
if cpu_choice == "rock":
print("It's a tie!")
elif cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Haha! I won!")
elif choice == "paper":
if cpu_choice == "rock":
print("Dang, I lost!")
elif cpu_choice == "paper":
print("It's a tie!")
elif cpu_choice == "scissors" or cpu_choice == "dynamite":
print("Haha! I won!")
elif choice == '"scissors':
if cpu_choice == "rock":
print("Haha! I won!")
elif cpu_choice == "scissors":
print("It's a tie!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Dang, I lost!")
elif choice == "dynamite":
if cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "dynamite":
print("It's a tie!")
elif cpu_choice == "paper" or cpu_choice == "rock":
print("Haha! I won!")
else:
print("Please use rock, paper, scissors, or dynamite!")

这两行代码是矛盾的:

cpu_choice = ["rock","paper","scissors","dynamite"]

cpu_choice = random.choice(cpu_choice)

当它是第一次运行时,它使用该列表,并将其指定为从中的一个选项;岩石;例如但第二次,它使用了";岩石;作为选择,而不是您之前声明的列表。只要稍微改变一下变量,你就可以开始了:

完整工作代码:

import random
choices = ["rock","paper","scissors","dynamite"]
for i in range(5):
choice = input("Your input: ").lower().strip("!.?")
cpu_choice = random.choice(choices)
if "rock" in choice:
if cpu_choice == "rock":
print("It's a tie!")
elif cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Haha! I won!")
elif "paper" in choice:
if cpu_choice == "rock":
print("Dang, I lost!")
elif cpu_choice == "paper":
print("It's a tie!")
elif cpu_choice == "scissors" or cpu_choice == "dynamite":
print("Haha! I won!")
elif "scissors" in choice:
if cpu_choice == "rock":
print("Haha! I won!")
elif cpu_choice == "scissors":
print("It's a tie!")
elif cpu_choice == "paper" or cpu_choice == "dynamite":
print("Dang, I lost!")
elif "dynamite" in choice:
if cpu_choice == "scissors":
print("Dang, I lost!")
elif cpu_choice == "dynamite":
print("It's a tie!")
elif cpu_choice == "paper" or cpu_choice == "rock":
print("Haha! I won!")
else:
print("Please use rock, paper, scissors, or dynamite!")

你说得对。写一大堆if/elif语句会导致非常难看的代码。有一种更有效的方法可以利用动作的循环层次来设计这款游戏。如果像{'r':0,'p':1,'d':2,'s':3}中那样为每个移动指定一个值,则您会意识到,当且仅当player1和player2的分数之差为1、2或-3时,player1获胜。您可以利用这一事实显著简化代码。以下代码运行5次:

import random
player_dic = {'r':0, 'p':1, 'd':2, 's': 3}
for i in range(5):
player_letter = input('Enter r or p or d or s: ')
player_number = player_dic[player_letter]
computer_choice = random.choice(list(player_dic.items()))
difference = (computer_choice[1] - player_number)
print(f'Player: {player_letter}, Computer: {computer_choice[0]}')
if difference == 0:
print("It's a tie!")
elif difference in [1,2,-3]:
print('Computer wins!')
else:
print('Player wins')

最新更新