if语句中没有认可第三条规则

时间:2017-12-13 16:49:44

标签: python

我是编码的新手,我一直在进行,只是在线进行随机挑战,以获得一般的python和编码,但是我遇到了一个挑战,但是我在某个地方难倒。

我正在尝试编写一些代码,你只需要用程序播放Rock,Paper,Scissors,但是当我选择剪刀时,即使我已经多次查看if语句并且一切似乎都是从我到目前为止看到的更正。

我对此代码的任何帮助,甚至只是对我的代码的建设性批评都非常感激,这取决于我如何构建代码或者做一些我已经详细阐述的简单方法。非常感谢你!

我的代码:

import random
import sys
import os
import time

clear = lambda: os.system('cls')

x = 0
y = 0

clear()

uChoice = raw_input("Rock, Paper, or Scissors?")

clear()

poss = ["Rock...", "Paper...", "Scissors..."]

while x < 3:
    print(poss[y])
    time.sleep(2)
    x = x + 1
    y = y + 1

time.sleep(2)

print("SHOOT!")
time.sleep(0.2)

sChoice = random.randrange(1,4)

if sChoice == 1:
    sChoicet = "Rock"
elif sChoice == 2:
    sChoicet = "Paper"
elif sChoice == 3:
    sChoicet = "Scissors"

if uChoice == "Rock" and sChoice == 1 or uChoice == "Paper" and sChoice == 2 or uChoice == "Scissors" and sChoice == "3":
    print("The opponent chose %s" %(sChoicet))
    print("The match was a draw.")
elif uChoice == "Rock" and sChoice == 2 or uChoice == "Paper" and sChoice == 3 or uChoice == "Scissors" and sChoice == "1":
    print("The opponent chose %s" %(sChoicet))
    print("You lost the match, unlucky.")
elif uChoice == "Rock" and sChoice == 3 or uChoice == "Paper" and sChoice == 1 or uChoice == "Scissors" and sChoice == "2":
    print("The opponent chose %s" %(sChoicet))
    print("You won the match!")

1 个答案:

答案 0 :(得分:0)

只需更改代码的结尾,如下所示:

if uChoice == "Rock" and sChoice == 1 or uChoice == "Paper" and sChoice == 2 or uChoice == "Scissors" and sChoice == 3:
    print("The opponent chose %s" %(sChoicet))
    print("The match was a draw.")
elif uChoice == "Rock" and sChoice == 2 or uChoice == "Paper" and sChoice == 3 or uChoice == "Scissors" and sChoice == 1:
    print("The opponent chose %s" %(sChoicet))
    print("You lost the match, unlucky.")
elif uChoice == "Rock" and sChoice == 3 or uChoice == "Paper" and sChoice == 1 or uChoice == "Scissors" and sChoice == 2:
    print("The opponent chose %s" %(sChoicet))
    print("You won the match!")

您正在将StringInteger值进行比较,这是比较永远不会成立的原因。