繁体   English   中英

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

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

我尝试制作一个数独求解程序。 我想用 if-else 语句获取匹配的变量(正变量)。 我举个例子。

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" ")

最好在多个if语句中执行,否则您无法确定用户输入了哪个值。 实施将是:

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

您还需要使用==进行比较,而不是= which 在作业中使用。

不知道是什么问题,这是你要找的东西:

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

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

暂无
暂无

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

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