danni_dougal
danni_dougal

Reputation: 11

Python while loop isn't doing as expected

I'm trying to complete my assignment and have been struggling. The idea is that you select report type, A or T. From there you enter keep entering integers until you quit. Once you quit, it should print out the total of integers added together for report 'T'; or for report 'A', it should print the total, plus a list of integers entered.

The problem I'm encountering at the moment is from report 'T', once I'm entering integers nothing will make it error or quit. It just keeps constantly asking me to enter another integer. Then from report 'A', every integer I enter it just comes up with 'invalid input'. I'm sure there are probably plenty more issues with my code but can't get past these ones at the moment. Any pointers would really be appreciated. Thanks

def adding_report(report):
    total = 0
    items = []
    while True:
        user_number = input("Enter an ingteger to add to the total or \"Q\" to quit: ")
        if report.upper == "A":
            if user_number.isdigit():
                total += int(user_number)
                items.append(user_number)
            elif user_number.upper() == "Q":
                break
            else:
                print("Invalid input\n")
        elif report.upper() == "T":
            if user_number.isdigit():
                total += int(user_number)
            elif user_number.upper() == "Q":
                break
        else:
            print("Invalid input\n")

report = input("Report types include All Items (\"A\") or Total Only (\"T\")\nPlease select report type \"A\" or \"T\": ")
while True:
    if report.upper() in "A T":
        adding_report(report)
    else:
        print ("Invalid input")
        report = input("Please select report type \"A\" or \"T\": ")

Upvotes: 0

Views: 117

Answers (3)

Chinonso Ngwu
Chinonso Ngwu

Reputation: 9

Try this

def adding_report(report):
      total = 0
      items = []
      while True:
        user_number = input("Enter an integer to add to the total or \"Q\" to quit: ")
        #You used "report.upper" instead of "report.upper()"
        if report.upper() == "A":
            if user_number.isdigit():
                total += int(user_number)
                items.append(user_number)
            elif user_number.upper() == "Q":
                break
            else:
                print("Invalid input\n")
        elif report.upper() == "T":
            if user_number.isdigit():
                total += int(user_number)
                #You forgot ot add this : "items.append(user_number)"
                items.append(user_number)
            elif user_number.upper() == "Q":
                break
        else:
            print("Invalid input\n")
            break
#Add this for loop termination: "or 0 to quit: "
report = input("Report types include All Items (\"A\") or Total Only (\"T\")\nPlease select report type \"A\" or \"T\" Or 0 to quit: ")
while True:
    #it should be this ""if report.upper() in "A" or "T":"" not this ""if report.upper() in "A T":""
    if report.upper() in "A" or "T":
        adding_report(report)
    #The condition below terminates the program
    elif report == '0':
        break
    else:
        print("Invalid input")
    report = input("Please select report type \"A\" or \"T\": ")

Upvotes: 0

Prune
Prune

Reputation: 77837

Your first problem is in this line:

    if report.upper == "A":

This always evaluates to False, because report.upper is a function object, not a value. You need

    if report.upper() == "A":

to return the value. You would also do well to rename the input variable and replace its value to the internal one you want:

report = input("Report types include All Items (\"A\") or Total Only (\"T\")\nPlease select report type \"A\" or \"T\": ")
report = report.upper()

This saves you the mess and time of calling upper every time you access that letter.

Please look through your code for repeated items and typos; you'll save headaches in the long run -- I know from personal experience.

Upvotes: 1

blue note
blue note

Reputation: 29071

The in operator needs a collection of possible values. Use

if report.upper() in ("A", "T")

or (closer to what you have)

if report.upper() in "A T".split()

Upvotes: 1

Related Questions