While loop creating

好久不见. 提交于 2019-12-14 03:32:21

问题


I am writing a program in Python that defines a function that takes a single argument. The function has to be a while loop the returns the largest power of 16 that it is equal to. However, I am not sure how to write the while loop.


回答1:


Python Docs

while True:
    n = input("Please enter 'hello':")
    if n.strip() == 'hello':
        break

so, in layman's terms

while <condition>:
    ...



回答2:


I couldn't completely understand your question but here's how to do a while loop to get the x to the 16th power of an input:

def loop_function(x):
    y = 1
    start = x
    while y != 16:
        result = start * x
        start = result
        y += 1
    return result

print loop_function(3)

The above code will return the answer to 3^16 which is 43046721

You can even make this a broader function two arguements

def loop_function(x, y):
    z = 1
    start = x
    while z != z:
        result = start * x
        start = result
        z += 1
    return result

print loop_function(3, 2)

The above code will return 9 i.e 3^2



来源:https://stackoverflow.com/questions/35472493/while-loop-creating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!