โปรแกรมที่ตามมายอมรับตัวเลขและดัชนีจากผู้ใช้ funcion แบบเรียกซ้ำ rpower() ใช้สองสิ่งนี้เป็นอาร์กิวเมนต์ ฟังก์ชันจะคูณตัวเลขซ้ำๆ และวนซ้ำเพื่อคืนกำลัง
ตัวอย่าง
def rpower(num,idx):
if(idx==1):
return(num)
else:
return(num*rpower(num,idx-1))
base=int(input("Enter number: "))
exp=int(input("Enter index: "))
rpow=rpower(base,exp)
print("{} raised to {}: {}".format(base,exp,rpow)) ผลลัพธ์
นี่คือตัวอย่างการทำงาน -
Enter number: 10 Enter index: 3 10 raised to 3: 1000