Python 即使条件满足,if循环中的程序也不工作
我制作了这个代码来模拟一个石头剪刀游戏。我有一个程序,它有主游戏,当我自己运行这个程序时,在最后没有if循环,它就工作了。但是,当我尝试验证用户条目是否为数字时,它不会打印正确的消息。以下是完整的代码:Python 即使条件满足,if循环中的程序也不工作,python,if-statement,Python,If Statement,我制作了这个代码来模拟一个石头剪刀游戏。我有一个程序,它有主游戏,当我自己运行这个程序时,在最后没有if循环,它就工作了。但是,当我尝试验证用户条目是否为数字时,它不会打印正确的消息。以下是完整的代码: import random def game(choice): number = random.randint(1,3) if choice == 1 and number == 2: print("You lose.") if choice == 1 a
import random
def game(choice):
number = random.randint(1,3)
if choice == 1 and number == 2:
print("You lose.")
if choice == 1 and number ==3:
print("You win.")
if choice == 1 and number == 1:
print("It is a draw")
if choice == 2 and number == 3:
print("You lose.")
if choice == 2 and number ==1:
print("You win.")
if choice == 2 and number == 2:
print("It is a draw")
if choice == 3 and number == 1:
print("You lose.")
if choice == 3 and number ==2:
print("You win.")
if choice == 3 and number == 3:
print("It is a draw")
x = input("Choose 1 (rock), 2 (paper) or 3 (scissors).")
digit = x.isdigit()
if digit == True:
game(x)
顺便说一下,没有错误消息,这个过程就是不工作 首先,您的代码存在一些问题。如果您希望忽略,请跳到错误处理以获得问题的答案。你会在那里找到它的 命名变量 变量不会告诉阅读代码的人它们是什么。如果你给某人看单词
数字
,他们就不会知道这是电脑在石头剪刀上选择的。与choice
(谁的选择?)相同,更何况x
。那么我们为什么不把它们分别命名为computer\u choice
、user\u choice
和choice
。我知道这些输入要长一点,但为了更清晰一点,牺牲一些额外的按键是值得的。还请注意,我使用了snake\u case
名称格式,这与通常的Python标准相同
重复性/game()
您的游戏
功能只需稍加修改即可。你所需要的只是一个输赢的通用“公式”。在这种情况下,失败是当(多亏@Brett Beatty)如果计算机选择==(用户选择+1)%3
时,绘图是当计算机选择==用户选择
时,而胜利就是一切。另外,在播放代码时,用户只知道他们赢了还是输了,而不知道计算机选择了什么,因此为了更清楚一点,我添加了一个列表和另一行。此功能对于测试代码也很有用。此外,使用基于零的变量值,例如在计算机选择
和用户选择
变量中。这在将值转换为列表索引时非常有用。下面是我所做代码的修订版本,其中包括以上所有要点(我还将其作为一个函数,您将在下面看到-我只是认为它看起来更好):
错误处理
要确保用户输入一个整数,您必须在输入语句周围添加int()
。但是,如果用户输入的值不是整数,则仅此操作将返回ValueError
,这是一个问题。但是,有一种方法可以使用try:except:else:
块修复此问题,如下所示。这是您需要确定该值是否为整数的内容,如果不是整数,它也会打印正确的消息。我还认为这是你想要的如果digit==True
(仅供参考,如果你这样做,你只需要如果digit
)
输出示例(忽略颜色):
另外,对于OP,如果您还没有,我建议您下载。它是完全免费的,并且使编码更加容易。它还改进了演示。它学习了上面提到的一些东西,并使您的代码符合PEP准则 您输入的
x
是字符串,而不是数字。您必须使用int()将其转换为整数。
@Piinthesky是正确的。你可能应该使用“elif”而不是大量的if语句,如果不满足任何条件,你应该在末尾使用include和“else”来打印某些内容。此外,你还可以使用if number==choice
对绘图进行轻微压缩,你也可以使用if choice==number:print(“这是一个绘图”);elif数字==(选项+1)%3:打印(“您输了”);else:print(“You win.”)
没有if循环这样的东西…很好的回顾,但它不能解释实际问题。实际问题不是循环的吗?等等。它正在决定输入是否是数字。我在错误处理部分谈到了这一点,请看问题。好的。这将增加答案
import random
def game(user_choice):
user_choice -= 1
rps = ["Rock", "Paper", "Scissors"]
computer_choice = random.randint(0, 2)
print("You: {} Computer: {}".format(rps[user_choice], rps[computer_choice]))
if user_choice == computer_choice:
return 0
elif computer_choice == (user_choice + 1) % 3:
return -1
else:
return 1
def main():
while True:
try:
value = int(input("Choose 1 (rock), 2 (paper) or 3 (scissors):"))
except ValueError: # If any part of the code in the try block raises a ValueError
print("Not an integer")
else: # If error is not raised
if not 1 <= value <= 3: # If value is not between 1 and 3
print("Must be between 1 and 3")
else:
result = game(value) # sets result to return value from game()
break
if result == 1:
print("You Win")
elif result == -1:
print("You Lose")
else:
print("It's a Draw")
if __name__ == '__main__':
while True:
main()
if input("Again? Yes(y) No(Anything else): ").lower() != 'y':
break
Choose 1 (rock), 2 (paper) or 3 (scissors):1
You: Rock Computer: Scissors
You Win
Again? Yes(y) No(Anything else): y
Choose 1 (rock), 2 (paper) or 3 (scissors):3
You: Scissors Computer: Scissors
It's a Draw
Again? Yes(y) No(Anything else): y
Choose 1 (rock), 2 (paper) or 3 (scissors):2
You: Paper Computer: Paper
It's a Draw
Again? Yes(y) No(Anything else): n
Process finished with exit code 0