Skip to main content
 首页 » 编程设计

Python "Rock, Paper, Scissors"验证

2025年05月04日8sxdcgaq8080

我一直在制作一款剪刀石头布游戏,它运行起来就像做梦一样。但是,当我尝试在(显示为 # 的)中添加一些验证时,我的游戏无法运行。我不确定为什么会这样。

我的代码如下:

    from random import randint 
    from sys import exit 
    computer = randint(1,3) 
 
    r = "r" 
    p = "p" 
    s = "s" 
    print ("The computer has chosen. Your turn") 
    player = input ("r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ") 
 
    #from here 
 
    if (player != r or p or s):  
            player = input ("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ") 
            if (player != r or p or s) : 
                    print ("Can you srsly not understand that " + player + " is not r, p, or s? I give up") 
                    exit() 
 
 
    #to here 
 
 
 
    if (computer == 1): 
            AI = ("rock") 
 
    if (computer == 2): 
            AI = ("paper") 
 
    if (computer == 3): 
            AI = ("scissors") 
 
    if (player == r and computer == 1): 
            print ("lol draw") 
            exit() 
 
    if (player == p and computer == 2): 
            print ("lol draw") 
            exit() 
 
    if (player == s and computer == 3): 
            print ("lol draw") 
            exit() 
 
    if (player == r and computer == 3): 
            print ("You WIN!!!!!! AI chose " + AI) 
 
    if (player == p and computer == 1): 
            print ("You WIN!!!!!! AI chose " + AI) 
 
    if (player == s and computer == 2): 
            print ("You WIN!!!!!! AI chose " + AI) 
 
    if (player == s and computer == 1): 
            print ("You LOSE!!!!!! AI chose " + AI) 
 
    if (player == r and computer == 2): 
            print ("You LOSE!!!!!! AI chose " + AI) 
 
    if (player == p and computer == 3): 
            print ("You LOSE!!!!!! AI chose " + AI) 

请您参考如下方法:

又是or运算符。

player != r or p or s 

应该是

player not in (r, p, s) 

或类似的。

解释:

A 或 B 的计算结果为 A,如果 A 被认为是真 (truey)。如果 A 被认为是假的(例如 False、0、0.0、[] , ''), A 或 B 的计算结果为 B。

player != r or p or s 与 (player != r) or p or s 相同。现在 (player != r) or p or s 评估为 True 如果 player != r 和 p否则。因为 True 和 p 都是“truey”,所以这两行是等价的:

if player != r or p or s: 
if True: