简体   繁体   English

在 Python 的 if-else 语句中是否可以打印响应变量?

[英]Is there any possible of printing responsive variables in if-else statement in Python?

I try to make a sudoku solver program.我尝试制作一个数独求解程序。 I want to get the variables who matches(positive-the true variables) with if-else statement.我想用 if-else 语句获取匹配的变量(正变量)。 I make an example.我举个例子。

a = int(input("Write a number (1-9)"))

b = int(input("Write a number (1-9)"))

c = int(input("Write a number (1-9)"))

d = int(input("Write a number (1-9)"))


if (a = 2 or b = 2 or c = 3 or d = 4):

    print("if the user prints 2 for "a" variable I want to print here "a = 2" ")

It is better to do in multible if statements, otherwise you can not determine which value has user entered.最好在多个if语句中执行,否则您无法确定用户输入了哪个值。 Implementation would be:实施将是:

if  a==2:
    print("a == 2")
elif b==2:
    print("b == 2")
elif c==3:
    print("c == 3")
elif d==4:
    print("d == 4")

Also you need to use == for comparing, not = which used in assignments.您还需要使用==进行比较,而不是= which 在作业中使用。

Not sure what is the question, is this something you're looking for:不知道是什么问题,这是你要找的东西:

    print('a=%s, b=%s, c=%s, d=%s' % (a, b, c, d))

This will print: a=1, b=2, c=3, d=4这将打印:a=1, b=2, c=3, d=4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM