iT邦幫忙

0

python 要怎麼同時輸出變數與字串

sja956 2019-07-20 21:39:3618764 瀏覽
  • 分享至 

  • xImage

如果把下面if後面的a,固定變數換成上面未輸入的x未知變數,也是一樣的操作嗎
https://ithelp.ithome.com.tw/upload/images/20190720/20119136MWUFqU9f0W.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
dragonH
iT邦超人 5 級 ‧ 2019-07-20 21:49:49

你直接試不就好了嗎...

x = input('input:')
x = int(x)
if x % 5 == 0:
  print(x, "是5的倍數")
elif x % 7 == 0:
  print(x, "是7的倍數")
else:
  print(x, "不是7的也不是5的倍數")

然後用 vscode 寫 python

建議你安裝他的套件

0
paicheng0111
iT邦大師 5 級 ‧ 2019-07-20 23:10:17

字串物件的Format方法

x = input('input:')
x = int(x)
if x % 5 == 0:
    print("{}是5的倍數".format(x))
elif x % 7 == 0:
    print("{}是7的倍數".format(x))
else:
    print("{}不是7的也不是5的倍數".format(x))
0
linlnnnn
iT邦新手 3 級 ‧ 2019-07-21 01:56:25
x = input("請輸入數字")
x = int(x)

if x % 5 == 0:
  print("%d是5的倍數" % x)
elif x % 7 == 0:
  print("%d是7的倍數" % x)
else:
  print("%d不是7的也不是5的倍數" % x)
0
chLee
iT邦見習生 ‧ 2019-07-21 22:48:50

Python >= 3.6,可使用 f-string

x = input("請輸入數字")
x = int(x)

if x % 5 == 0:
    print(f"{x} 是5的倍數")
elif x % 7 == 0:
    print(f"{x} 是7的倍數")
else:
    print(f"{x} 不是7的也不是5的倍數")

我要發表回答

立即登入回答