python(和,或)中逻辑语句的组合

时间:2018-03-17 01:39:43

标签: python tkinter logic

我现在正在tkinter制作摇滚,纸张,剪刀射击游戏,我正在尝试编写一个决定谁获胜的功能。 1 =岩石,2 =纸,3 =剪刀。除了comp == 2和choice == 1。

之外,脚本不会打印任何内容
requiredAssemblies.Keys

2 个答案:

答案 0 :(得分:2)

我认为你走错了方向。假设我们必须使用数字输入,我会这样做:

ROCK, PAPER, SCISSORS = range(1, 4)

roshambo = {
    ROCK: SCISSORS,
    PAPER: ROCK,
    SCISSORS: PAPER
}

def Winning(comp, choice):
    if comp == choice:
        messagebox.showinfo("Info", "DRAW!!!")
    elif comp == roshambo[choice]:
        messagebox.showinfo("Info", "YOU WIN!!!")
    else:
        messagebox.showinfo("Info", "YOU LOSE!!!")

这使您的逻辑清晰并简化了代码。并且,它可以更容易地扩展问题:

ROCK, PAPER, SCISSORS, LIZARD, SPOCK = range(1, 6)

roshambo = {
    ROCK: [LIZARD, SCISSORS],
    PAPER: [ROCK, SPOCK],
    SCISSORS: [PAPER, LIZARD],
    LIZARD: [SPOCK, PAPER],
    SPOCK: [SCISSORS, ROCK]
}

def Winning(comp, choice):
    if comp == choice:
        messagebox.showinfo("Info", "DRAW!!!")
    elif comp in roshambo[choice]:
        messagebox.showinfo("Info", "YOU WIN!!!")
    else:
        messagebox.showinfo("Info", "YOU LOSE!!!")

字典是你的朋友,没有它们就不会编程!

答案 1 :(得分:0)

你在哪里获得compchoice

这样的事情会起作用:

from tkinter import messagebox

rock=1
paper=2
scissors=3


def Winning(comp, choice):
    if (((comp == 2) and (choice == 1)) or ((comp == 3) and (choice == 2)) or((comp == 1) and (choice == 3))):
        messagebox.showinfo("Info", "YOU LOSE!!!")
    if (((choice == 2) and (comp == 1)) or ((choice == 3) and (comp == 2)) or((choice == 1) and (comp == 3))):
        messagebox.showinfo("Info", "YOU WIN!!!")
    else:
        messagebox.showinfo("Info", "DRAW!!!")


Winning(rock, paper)

这是你想要的吗?