簡體   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