【问题标题】:Trying add two inputs in while尝试在 while 中添加两个输入
【发布时间】:2021-10-20 14:19:24
【问题描述】:

我正在用 python 进行算术级数。 在前 10 个术语之后,我想询问用户还想查看多少个术语。如果为 0,则程序结束。如果不是程序,则返回所询问的术语数。并重复直到它为 0。 在第一个循环之后,程序运行,之后程序返回一个空。

i = int(input('Start of PA: '))
r = int(input('PA Reason:  '))
t1 = i
cont = 1
terms = 1
n1 = 0
while cont <= 10:
    t1 = t1 + r
    cont += 1
    print(f'{t1} > ', end='')
while terms != 0 :
    terms = int(input('\nHow many terms? '))
    if terms!= 0:
        while cont <= (10 + terms):
            t1 = t1 + r
            cont += 1
            print(f'{t1} > ', end='')
    else:
        print('END!')

resolution

编辑:对不起我的英语。

【问题讨论】:

  • 问题是什么?你需要什么?

标签: python input while-loop is-empty arithmetic-expressions


【解决方案1】:
while cont <= (10 + terms):

只是第二批结果的正确条件。下一批应该小于20 + terms,以此类推。

您应该在打印下一批术语的每个循环之前将cont 设置回0,而不是添加到terms

与其检查terms 是否为0 两次,不如使用while True: 并在他们输入0 时跳出循环。

while True:
    terms = int(input('\nHow many terms? '))
    if terms!= 0:
        cont = 0
        while cont <= terms:
            t1 = t1 + r
            cont += 1
            print(f'{t1} > ', end='')
    else:
        print('END!')
        break

或者不使用while 循环,而是使用forrange()

while True:
    terms = int(input('\nHow many terms? '))
    if terms != 0:
        for _ in range(terms)
            t1 = t1 + r
            print(f'{t1} > ', end='')
    else:
        print('END!')
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多