我的程序要求用户输入一个幂和他们想要的位数。并找到用户输入的幂的最后的2位数字。
我的代码如下所示。我只是python的初学者。我没有得到所需的输出。
temp = int(input('Enter the power of the number: '))
temp2 = int(input('Enter the no.of digits you want: '))
temp3 = (2 ** temp) // temp2
temp4 = (temp3 % 100)
print('The last that many digits of the number raised to the power is is:',temp4)发布于 2019-11-19 23:20:09
我假设你正在寻找类似这样的东西:
功率:8
位数:2
2^8= 256
最后两位数= 56
要做到这一点,您的代码应如下所示:
power = int(input('two to the power of '))
digits = int(input('last how many digits? '))
num = 2 ** power # calculate power
num = num % (10 ** digits) # get remainder of division by power of 10
print(num)这是另一种方法:
power = int(input('two to the power of '))
digits = int(input('last how many digits? '))
num = 2 ** power # calculate power
num = str(num) # convert with string to work with
num = num[-digits:] # get last n digits
num = int(num)
print(num)https://stackoverflow.com/questions/58936800
复制相似问题