[英]Python exponent operator and multiplication
我正在嘗試進行指數乘法運算,但 output 不是我所期望的。 它應該是一個簡單的操作,程序接受一個數字,然后乘以 10³。
輸入:9 預期 output:9000
實際 output:
這是我的代碼:
num = input("Please enter a number:")
n1 = 10**3
output = n1*num
print(output)
當您將str
乘以int
時,字符串會重復:
>>> "foobar" * 10
'foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar'
您將字符串"9"
乘以數字1000
,這意味着您得到一千個 "9" 的字符串。
如果您想將數字9
乘以數字1000
,那看起來更像:
num = int(input("Please enter a number:"))
n1 = 10**3
print(n1*num)
嘗試這個。
num = int(input("Please enter a number:"))
output = (10 ** 3) * num
print(output)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.