Python Forum

Full Version: help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please someone help me with this:
initialize total variable which will sum integer values entered

initialize items variable which will build a string of the integer inputs separated with a new line character

define the adding_report function with one parameter report that will be a string with default of "T"

inside the function build a forever loop (infinite while loop) and inside the loop complete the following

use a variable to gather input (integer or "Q")
check if the input string is a digit (integer) and if it is...
add input iteger to total
if report type is "A" add the numeric character(s) to the item string seperated by a new line
if not a digit, check if the input string is "Q" or starts with a "Q", if "Q" then...
if the report type is "A" print out all the integer items entered and the sum total
if report type is "T" then print out the sum total only
break out of while loop to end the function after printing the report ("A" or "T")
if not a digit and if not a "Q" then print a message that the "input is invalid"
Call the adding_report function with "A" and then with "T" report parameters
Welcome to the forums. We are glad to help, but we are not going to do it for you. Post your code in python tags, full traceback if you get any exceptions - in error tags. Ask specific questions.
Also, please use descriptive thread names...
Hello,
thank a lot Buran and I'm so sorry but this my fisrt experience in Python
so I already star it but i dont know why it's not running.
kindly get a look for me.

here is the code:

total = 0
itmes = 0
def adding_report(T):
    while True:
        report = input('enter an integer or "Q" :')
        if report.isdigit() == True:
            total = total + report
            if report_type == "A":
                items = report + '\n'
        elif report.startswith("Q") == True:
                if report_type == "A":
                    print('Items', '\n')
                    print('Total','\n', total)
                elif type_report == "T":
                    print('Total','\n',total)
                else:
                    break
        else:
            print(report, "is an invalid ipunt")
            
            adding_report(type_report = input('Choose Report Type ("A" or "T"): \n' + ' Reports Types inculde all Items ("A") or Total Only ("T") '))
type_report = input('Choose Report Type ("A" or "T"): \n' + ' Reports Types inculde all Items ("A") or Total Only ("T") ') 
def adding_report(T): 
    total = 0 
    itmes = 0
    while True:  
        report = input('enter an integer or "Q" :') 
        if report.isdigit() == False: 
            total = total + int(report) 
            if type_report == "A": 
                items = items + report + '\n' 
            elif report == "Q": 
                if type_report == "A": 
                    print('Items','\n') 
                    print('Total','\n', total) 
                elif type_report == "T": 
                        print('Total','\n',total) 
                else: 
                    print(report, "is an invalid input") 
                    break 
        else: 
            print(report, "is an invalid input")
adding_report("T")
Output:
Choose Report Type ("A" or "T"): Reports Types inculde all Items ("A") or Total Only ("T") A enter an integer or "Q" :4 4 is an invalid input enter an integer or "Q" :5 5 is an invalid input enter an integer or "Q" :Q
Error:
ValueError Traceback (most recent call last) <ipython-input-5-ed886f766dcf> in <module>() 23 print(report, "is an invalid input") 24 ---> 25 adding_report("T") <ipython-input-5-ed886f766dcf> in adding_report(T) 8 report = input('enter an integer or "Q" :') 9 if report.isdigit() == False: ---> 10 total = total + int(report) 11 if type_report == "A": 12 items = items + report + '\n' ValueError: invalid literal for int() with base 10: 'Q'
Error message quite clearly explains the problem.
You enter "Q", which gets stored to variable report. Then the code tries to convert "Q" to integer in line 8.

Are you sure you want to check for "False" in line 7?
Hello Thx a lot j.crater
It's working now but I have another problem.
In the output, for the Items i don't have the list of entry..
Kindly get a look for me.

type_report = input('Choose Report Type ("A" or "T"): \n' + ' Reports Types inculde all Items ("A") or Total Only ("T") ')
def adding_report(T):
    total = 0
    itmes = 0
    while True:
        report = input('enter an integer or "Q" :')
        if report.isdigit() == True:
            total =  total + int(report)
            if type_report == "A":
                items = report
        elif report.startswith("Q") == True:
                if type_report == "A":
                    print('Items','\n',items,'\n')
                    #print(items, '\n')
                    print('Total','\n', total)
                elif type_report == "T":
                    print('Total','\n',total)
                else:
                    print(report, "is an invalid input")
                break
        else:
            print(report, "is an invalid input")
                        
adding_report("A")[output][/output]
you need items to be a list, not integer. then you can append user input to this list if report type is A.

few more points - use meaningful variable names. instead of report it's much better to use item - that's singular for items, right? :-)

also, they don't want you to ask for user input for report type. Read again this two parts of the assignment
define the adding_report function with one parameter report that will be a string with default of "T"
Call the adding_report function with "A" and then with "T" report parameters

that's not what you have so far
ok Buran thx a lot
let's try to see what i'll get.
Hello Guys,
Thx a lot for your support I've juste submit the code an it's working good now thx a lot.

type_report = input('Choose Report Type ("A" or "T"): \n' + ' Reports Types inculde all Items ("A") or Total Only ("T") ')
def adding_report(T):
    total = 0
    items = '' 
    while True:
        report = input('enter an integer or "Q" :')
        if report.isdigit() == True:
            total =  total + int(report)
            if type_report == "A":
                items += report+ '\n'
        elif report.startswith("Q") == True:
                if type_report == "A":
                    print('Items','\n',items)
                    print('Total','\n', total)
                elif type_report == "T":
                    print('Total','\n',total)
                else:
                    print(type_report, "is an invalid input")
                break
        else:
            print(report, "is an invalid input")
                        
adding_report("A")
Output:
Choose Report Type ("A" or "T"): Reports Types inculde all Items ("A") or Total Only ("T") A enter an integer or "Q" :5 enter an integer or "Q" :6 enter an integer or "Q" :4 enter an integer or "Q" :2 enter an integer or "Q" :1 enter an integer or "Q" :67 enter an integer or "Q" :Q Items 5 6 4 2 1 67 Total 85