代码:
def power(base,exponent):
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
n=raw_input("Enter a number whose power you wish to calculate:")
p=raw_input("Enter the power:")
power(n,p)
执行请帮助时出现一些unicode错误
答案 0 :(得分:0)
raw_input
返回字符串。你必须将它转换为整数,因为你的函数找到一个数字的力量。
def power(base,exponent):
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
n=int(raw_input("Enter a number whose power you wish to calculate:"))
p=int(raw_input("Enter the power:"))
power(n,p)
答案 1 :(得分:0)
您也可以使用它来确保在插入非数字时不会出错:
def power(base,exponent):
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
def int_input(message):
input_value = raw_input(message)
if input_value.isdigit():
return int(input_value)
print "This is not a Number."
return int_input(message)
n = int_input("Enter a number whose power you wish to calculate:")
p = int_input("Enter the power:")
power(n, p)