【问题标题】:Need to convert this For loop to while需要将此 For 循环转换为 while
【发布时间】:2022-01-19 04:52:27
【问题描述】:

我正在尝试在 python 中将 for 循环转换为 while 循环,但我不太确定该怎么做。这里需要一些帮助,谢谢!这就是我正在使用的:

#FOR #aplicacion que imprima tablas de multiplicar desde la tabla que yo quiera hasta la tabla que yo quiera elijiendo desde que multiplicacion mostrar hasta que multiplicacion mostar

desde=int(input("desde tabla quiere saber...?"))
hasta=int(input("hasta que tabla quiere saber...?"))
comenzando=int(input("desde que multiplicacion quiere ver?...?"))
multi=int(input("hasta que multiplicacion quiere ver?...?"))

for i in range(desde,hasta+1):
    for a in range(comenzando,multi+1):
        rta= i*a

【问题讨论】:

  • 什么不清楚?你为什么不尝试编写循环并检查它是否有效..然后问你的疑问?

标签: python for-loop while-loop


【解决方案1】:

这是来源

desde=int(input("desde tabla quiere saber...?"))
hasta=int(input("hasta que tabla quiere saber...?"))
comenzando=int(input("desde que multiplicacion quiere ver?...?"))
multi=int(input("hasta que multiplicacion quiere ver?...?"))
x = desde
while x <= hasta:
    a = comenzando
    while a <= multi:
        rta = x*a

        a += 1
    x += 1
print(rta)

【讨论】:

    【解决方案2】:

    for 非常适合您拥有的循环,因为您知道开始和结束并且有固定数量的循环。 while 更适合可以运行不同数量循环的循环,例如重复提示,直到输入有效的电子邮件地址。

    i = desde
    while i <= hasta:
    
        a = comenzando
        while a <= multi:
            rta = i*a
    
            a += 1
        i += 1
    

    这可以通过一个while 循环来完成,但需要计算出ai 的值,使用除法和模/余数。

    【讨论】:

    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2016-07-01
    • 2018-02-22
    相关资源
    最近更新 更多