Process multiple input values in Python

后端 未结 2 1511
轻奢々
轻奢々 2021-01-27 18:51

Hi so I\'m very very new to programming so please forgive me if I\'m not able to ask with the correct jargon, but I\'m writing a simple program for an assignment in which I\'m t

2条回答
  •  死守一世寂寞
    2021-01-27 19:02

    Sure! You'll have to provide some sort of "marker" for when you're done, though. How about this:

    if num == '1':
        lst_of_nums = []
        while True: # infinite loops are good for "do this until I say not to" things
            new_num = raw_input("Enter value: ")
            if not new_num.isdigit():
                break
                # if the input is anything other than a number, break out of the loop
                # this allows for things like the empty string as well as "END" etc
            else:
                lst_of_nums.append(float(new_num))
                # otherwise, add it to the list.
        results = []
        for num in lst_of_nums:
            results.append(num/1e4)
        # this is more tersely communicated as:
        #   results = [num/1e4 for num in lst_of_nums]
        # but list comprehensions may still be beyond you.
    

    If you're trying to enter a bunch of comma-separated values, try:

    numbers_in = raw_input("Enter values, separated by commas\n>> ")
    results = [float(num)/1e4 for num in numbers_in.split(',')]
    

    Hell if you wanted to list both, construct a dictionary!

    numbers_in = raw_input("Enter values, separated by commas\n>> ")
    results = {float(num):float(num)/1e4 for num in numbers_in.split(',')}
    for CGS,SI in results.items():
        print "%.5f = %.5fT" % (CGS, SI)
        # replaced in later versions of python with:
        #   print("{} = {}T".format(CGS,SI))
    

提交回复
热议问题