获取输入数组,执行一个数学函数,然后循环执行另外3个函数

2024-04-27 04:08:12 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我一直坚持的任务,我用python来实现它:

First, take the input array values from the first loop sequence and set them up so that a mathematical function (like add) is done with the values, then output to screen. Loop back to do a different function with the values. In total, use all four functions. Take a screenshot and note on the results.

这是我当前的代码:

a = input('Enter a value: ')
b = input('Enter a value: ')
c = input('Enter a value: ')
d = input('Enter a value: ')
e = input('Enter a value: ')
f = input('Enter a value: ')
g = input('Enter a value: ')
h = input('Enter a value: ')
i = input('Enter a value: ')
j = input('Enter a value: ')
list1 = [a, b, c, d, e, f, g, h, i, j]


again = input("Do you want to do some math?")
if again == "y":

        add = (list1 + list1)
        print (add)

        again = input ("Want to play again? y/n")

elif again == "n":
        sub = (list1 - list1)
        print (sub)


else:
        mult = (list1 * list1)
        print (mult)
break 

我需要用户输入10个数字,这是保存到一个数组变量。在那之后,我需要创建一个循环来执行一个数学函数(即Add),然后循环回来执行另外3个函数(即Sub、Mult和Div)。你知道吗


Tags: andthetoaddinputvaluewithfunction
2条回答

我想这就是你的要求:

#This is where I will store all the values of the inputs
values=[]
#Here i will use a for loop to loop through and ask the user several times to grab some user intger data
for loop in range(10):
    values.append(int(input('Enter a value: '))) #I then ask the user 11 times for an interger value which I then append to list called values
#The rest you should be able to figure it out...

again = input("Do you want to do some addition?(y/n)")
if again == "y":
        add=0
        for value in values:
            add+=value
        print (add)

        again = input ("Subtract=y multiply=n)")

if again == "y":
        sub=0
        for value in values:
            sub-=value
        print (sub)


else:
    mult=1
    for value in values:
        mult*=value
    print (mult)

有什么问题请问!你知道吗

我相信这就是你想要的,虽然可能数学函数可能不同,但无论哪种方式,如果你用模块化的方式做东西会更好,这样更容易阅读

def get_numeric_input():
    try:
        return int(raw_input('Insert a number: '))
    except ValueError:
        print 'A number must be entered'
        return get_numeric_input()


def get_answer(again=None):
    if not again:
        answer = raw_input('Do you want to do some math? ')
    else:
        answer = raw_input('Do you want to play again? ')
    if answer in ['y', 'Y']:
        return True
    elif answer in ['n', 'N']:
        return False
    print 'Either Y or N are the accepted answers'
    return get_answer(again)


def perform_math(iteration, inputs):
    functions = ['Add', 'Sub', 'Mult']
    print functions[iteration]
    if iteration == 0:
        return sum(inputs)
    elif iteration == 1:
        return sum([inputs[0]] + [-x for x in inputs[1:]])
    elif iteration == 2:
        return reduce(lambda x,y: x*y, inputs)
    raise ValueError



inputs = []
for _ in xrange(10):
    inputs.append(get_numeric_input())

again = False
for i in xrange(3):
    if get_answer(again):
        again = True
        print perform_math(i, inputs)
    else:
        break

相关问题 更多 >