【问题标题】:Adding while loop user inputs calculations to a list. Python3将 while 循环用户输入计算添加到列表中。 Python3
【发布时间】:2023-03-25 02:33:01
【问题描述】:

我正在使用 python3,并且我有这段代码,我在其中向用户询问三个输入。然后我对它们进行计算。我想继续将计算结果添加到列表中。该怎么做?

...

if choice == '1': #my fist input x
    while True:
        x = int(input('Please insert the number of things you consider yourself a fan of'))
        if x > 0:
            break
        else:
             print('Please insert a number higher than 0')

elif choice == '2': #my second input y
    while True:
        y = int(input('Please insert the number of hobbies you like to do every month'))
        if y % 4 == 0:
            break
        else:
            print('Please insert a valid number')

elif choice == '3': #my third input z
    while True:
        z = int(input('Please insert the number of sports you like'))
        if z > 0:
            break
        else:
            print('Please insert a number higher than 0')

elif choice == '4': #the calculation part
    import math
    def square_root():
        c=(42 * y ** 2)/(z + 1)
        nerd_score=int(x*math.sqrt(c))
        return nerd_score
    print('your nerd score is', square_root())

我希望循环继续进行,并将每个结果添加到列表中。直到用户退出循环。

【问题讨论】:

  • 创建一个列表,即list = [],然后像list.append(result) 这样附加你的结果我想这就是你所要求的。
  • 如果我这样做,我只会得到一个分数。由于这是一个循环,我希望用户能够添加多组 input(x,y,z) 并且每个结果都将添加到一个列表中。
  • 您是否要返回设置选项的主循环并继续收集值直到选择 4?您应该扩展您的代码摘录以包含主循环,以便我们拥有完整的图片
  • 我想收集三个数字然后在4处得到结果。然后我想继续将结果添加到列表中。

标签: python list loops while-loop


【解决方案1】:

据我了解,你要解决的问题有两个:

  1. 循环继续,util用户想退出循环
  2. 将每个结果添加到列表中

示例代码在这里:

def loop_calculation(choice):
    # record results in list
    results = []

    # loop keep going, util user input choice 'x' which means exit
    while choice != 'x':
        if choice == '1':  # my fist input x
            while True:
                x = int(input('Please insert the number of things you consider yourself a fan of'))
                if x > 0:
                    break
                else:
                    print('Please insert a number higher than 0')

        elif choice == '2':  # my second input y
            while True:
                y = int(input('Please insert the number of hobbies you like to do every month'))
                if y % 4 == 0:
                    break
                else:
                    print('Please insert a valid number')

        elif choice == '3':  # my third input z
            while True:
                z = int(input('Please insert the number of sports you like'))
                if z > 0:
                    break
                else:
                    print('Please insert a number higher than 0')

        elif choice == '4':  # the calculation part
            import math

            def square_root():
                c = (42 * y ** 2) / (z + 1)
                nerd_score = int(x * math.sqrt(c))
                return nerd_score

            result = square_root()
            print('your nerd score is', result)

            # add each result to list
            results.append(result)

    return results

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 2019-05-19
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2013-02-27
    • 2016-01-02
    • 2021-09-14
    • 2020-06-08
    相关资源
    最近更新 更多