iT邦幫忙

1

請問一下Python 要如何印出(+數字)

https://ithelp.ithome.com.tw/upload/images/20200603/20127646TAoywlWaHI.png

x=int(input())
print("Original:","%.2f"%(x))
print("Adjusted:","%.2f"%(x0.5*10),end="")
print("+",(round(x
0.5*10)-x))
這是我打的,無法印出括號

用FORMAT

print( "(+{0})".format(round(x0.5*10)-x))
你...上...面...不...就...有...用...了...
print("(+%d)" % (round(x * 0.5 * 10) - x))
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
通靈亡
iT邦高手 1 級 ‧ 2020-06-03 10:21:53
最佳解答

記得用</>貼程式碼,你的開根號跑掉了

# %d
print("(+%d)"%(round(x ** 0.5 * 10)-x))
# format
print( "(+{0})".format(round(x ** 0.5 * 10)-x))
# 字串相接
print("(+" + str(round(x ** 0.5 * 10)-x) + ")")
0
褲底農民
iT邦新手 4 級 ‧ 2020-06-03 12:54:49

我來補上f-string的寫法

x = int(input())
print(f'Original:{x:.2f}')
print(f'Adjusted:{x**0.5*10:.2f}', end='')
print(f'({round(x**0.5*10-10):+.2f})')
0

我總合各位專家的程式碼,
做了以下的測試

最後試出來最接近的程式碼,

Coding 如下:

x = int(input())
print(f'Original:{x:.2f}')
print(f'Adjusted:{x**0.5*10:.2f}', end='')
print(f'({round(x**0.5*4):+.2f})')

照片: 輸入36 輸出跟題目一樣
https://ithelp.ithome.com.tw/upload/images/20200604/20126723kKQzsG4sSo.jpg

照片: 輸入50 輸出70.71(+28) 但是 題目需求是(+21) ,誤差7

https://ithelp.ithome.com.tw/upload/images/20200604/20126723ILGwofE1nk.jpg

關於我
www.okna.tw

我要發表回答

立即登入回答