Python字符串迭代/函数

时间:2017-11-18 12:45:19

标签: python function

我需要我的'items'变量来打印新行上的每个项目。我总是在'项目'和'总'中得到总数。 'total'打印出我想要的样子,但我希望单独打印这些项目。

思想?

def adding_report():
    user_input = input("Report Types include All Items ('A') or Total Only ('T')\nChoose Report Type ('A'or'T'):") 
    items = "\n"
    total = 0
    while True:
        if user_input == 'A'.lower():
            user_input1 = input("Input an integer to add to the total or 'Q' to quit: ")
            if user_input1.isdigit():
                items = int(user_input1)
                total += int(user_input1)
                continue
            elif user_input1 == 'Q'.lower():
                print("Items\n", items)
                print("Total\n", total)
                break
            elif user_input1.startswith('q'):
                print('Items\n', int(items))
                print("Total\n", total)
                break
            else:
                print("Input is not valid")
        elif user_input == 'T'.lower():
            user_input2 = input("Input an integer to add the total or 'Q' to quit: ")

adding_report()

3 个答案:

答案 0 :(得分:0)

user_input = raw_input("Report Types include All Items ('A') or Total Only ('T')\nChoose Report Type ('A'or'T'):")
items=[]
total = 0
print user_input
total = 0
while True:
    user_input1 = raw_input("Input an integer to add to the total or 'Q' to quit: ")
    if user_input == 'A'.lower() or user_input == "A":
        if user_input1.isdigit():
            items.append(int(user_input1))
            total += int(user_input1)
            continue
        elif user_input1 == 'Q'.lower() or user_input1 =="Q" or user_input1.startswith('q'):
            print "List of items is"
            for item in items:
                print item
            print "Total ", total
            break
        else:
            print("Input is not valid")
    elif user_input == 'T'.lower() or user_input == "T":
        if user_input1.isdigit():
            items.append(int(user_input1))
            total += int(user_input1)
            continue
        elif user_input1 == 'Q'.lower() or user_input1 =="Q" or user_input1.startswith('q'):
            print "Total ",total
            break
        else:
            print("Input is not valid")

答案 1 :(得分:0)

这是修改后的代码。很难准确理解你的脚本应该做什么:

import textwrap # nice library to format text inside functions

def adding_report():

    # initiate variables
    total = 0
    items = 0

    # Make sure you get the right input
    while True:
        user_input = input(textwrap.dedent("""\
            Report Types include All Items ('A') or Total Only ('T')
            Choose Report Type ('A'or'T'):""")) 
        if user_input list("AT"):
            break

    # Create a loop where you ask the user for input
    # Q or q quits (the print is outside the function)
    while True:
        user_input1 = input("Input an integer to add to the total or 'Q' to quit: ")
        if user_input1.lower() == 'q':
            break
        if user_input1.isdigit():              
            if user_input == 'A':
                items = int(user_input1)
                total += int(user_input1)
            elif user_input == 'T':
                total += int(user_input1)
        else:
            print("Input is not valid")

    # Return variables   
    return items,total


items,total = adding_report()
print("Items\n", items)
print("Total\n", total)

答案 2 :(得分:0)

这是我尝试理解你要做的事情:

print("Report Types includes All Items ('A') or Total Only ('T')")
report_type_raw = input("Choose Report Type ('A' or 'T'): ")

report_type = report_type_raw.lower()

if report_type in 'at':
    items = []
    total = 0

    user_input = ''

    while user_input != 'q':
        user_input_raw = input("Input an integer to add to the total or 'Q' to quit: ")
        if user_input_raw.isdigit():
            current_item = int(user_input_raw)

            if report_type == 'a':
                items.append(user_input_raw)

            total += current_item
            user_input = ''
        else:
            user_input = user_input_raw.lower()
            if user_input != 'q':
                print("Input is not valid")

    if report_type == 'a':
        how_items_to_be_printed = ', '.join(items)
        print("Items :", how_items_to_be_printed)

    print("Total :", total)
else:
    print("Report type is not valid")

否则你必须澄清你想要做的事情。