Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这个石头剪刀布游戏不起作用?_Python_Python 3.x - Fatal编程技术网

Python 为什么这个石头剪刀布游戏不起作用?

Python 为什么这个石头剪刀布游戏不起作用?,python,python-3.x,Python,Python 3.x,我正在做一个石头,剪纸游戏表单应用程序和代码运行。然而,我似乎无法让它正常工作。用户将键入纸、剪刀或石头,然后计算机也将从选项列表中选择其中一种。它有时说你赢了,但你真的输了。这个代码有什么问题 from sys import exit from time import sleep from random import choice options = ["rock", "paper", "scissors"] print("rock") sleep(0.5) print("paper")

我正在做一个石头,剪纸游戏表单应用程序和代码运行。然而,我似乎无法让它正常工作。用户将键入纸、剪刀或石头,然后计算机也将从选项列表中选择其中一种。它有时说你赢了,但你真的输了。这个代码有什么问题

from sys import exit
from time import sleep
from random import choice

options = ["rock", "paper", "scissors"]

print("rock")
sleep(0.5)
print("paper")
sleep(0.5)
print("scissors")
sleep(0.5)
user_move = input("shoot: ")

cpu_move = choice(options)
print(cpu_move)

if cpu_move == "rock":
    if user_move == "rock" or "r":
        print("Tie!")
        exit(0)
    elif user_move == "paper" or "p":
        print("You Lose!")
        exit(0)
    elif user_move == "scissors" or "s":
        print("You Win!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "paper":
    if user_move == "rock" or "r":
        print("You Win!")
        exit(0)
    elif user_move == "paper" or "p":
        print("Tie!")
        exit(0)
    elif user_move == "scissors" or "s":
        print("You Lose!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "scissors":
    if user_move == "rock" or "r":
        print("You Lose!")
        exit(0)
    elif user_move == "paper" or "p":
        print("You Win!")
        exit(0)
    elif user_move == "scissors" or "p":
        print("Tie!")
        exit(0)
    else:
        print("error")
        exit(1)

else:
    print("error")
    exit(1)

正如@SimonR所提到的,你的比较是反向的。打印CPU的移动,并比较打印“赢/平/输”,就好像玩家是
CPU\u move


帮助避免这些逻辑错误的一种方法是简化代码。即使你一个接一个地修复了它,你也会有很多失败的机会-查看所有相关问题,以找到构建此结构的替代方法。

你检查用户移动的方式是不正确的。此外,您还需要在任何地方反转您正在打印的内容,因为目前,它是从计算机的角度进行的

这可以通过以下方式完成和修复:

from sys import exit
from time import sleep
from random import choice

options = ["rock", "paper", "scissors"]

print("rock")
sleep(0.5)
print("paper")
sleep(0.5)
print("scissors")
sleep(0.5)
user_move = input("shoot: ")

cpu_move = choice(options)
print(cpu_move)

if cpu_move == "rock":
    if user_move in ["rock", "r"]:
        print("Tie!")
        exit(0)
    elif user_move in ["paper", "p"]:
        print("You Win!")
        exit(0)
    elif user_move in ["scissors", "s"]:
        print("You Lose!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "paper":
    if user_move in ["rock", "r"]:
        print("You Lose!")
        exit(0)
    elif user_move in ["paper", "p"]:
        print("Tie!")
        exit(0)
    elif user_move in ["scissors", "s"]:
        print("You Win!")
        exit(0)
    else:
        print("error")
        exit(1)

elif cpu_move == "scissors":
    if user_move in ["rock", "r"]:
        print("You Win!")
        exit(0)
    elif user_move in ["paper", "p"]:
        print("You Lose!")
        exit(0)
    elif user_move in ["scissors", "s"]:
        print("Tie!")
        exit(0)
    else:
        print("error")
        exit(1)

else:
    print("error")
    exit(1)

不久前,我还在
python
中做了同样的游戏

如果用户_move==“rock”或用户_move==“r”:
等等。你不把你的条件放在后面吗?如果cpu玩石头游戏,剪刀输了,纸赢了。@Ayush Gupta我的答案能解决你的问题吗?如果是,请单击勾号关闭问题…@Ayush Gupta如果解决了您的问题,请将答案标记为已接受,或者如果您仍有任何问题,请告诉我。