Python Forum
Trouble with edX Python Final
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with edX Python Final
#1
Hello Everyone,

I am currently taking the edX course "Introduction to Python: Absolute Beginner" through Microsoft (Course ID DEV236x), and am having trouble getting the code to run for my final assignment.

Here are the instructions ---------------------------------------------------------------------------------


Program: adding_report() function

This program calls the adding_report() function which repeatedly takes positive integer input until the user quits and then sums the integers and prints a "report".
The adding_report() function has 1 string parameter which indicates the type of report:

"A" used as the argument to adding_report() results in printing of all of the input integers and the total
"T" used as the argument results in printing only the total
Sample input and output:

call adding_report() with "A" as argument (print all the integers entered and the total)

Input an integer to add to the total or "Q" to quit
Enter an integer or "Q": 3
Enter an integer or "Q": 6
Enter an integer or "Q": 24
Enter an integer or "Q": 17
Enter an integer or "Q": 61
Enter an integer or "Q": nine
nine is invalid input
Enter an integer or "Q": q

Items
3
6
24
17
61

Total
111
call with "T"(print only the total)

Input an integer to add to the total or "Q" to quit
Enter an integer or "Q": 5
Enter an integer or "Q": 7
Enter an integer or "Q": Quit

Total
12


And here is my code so far --------------------------------------------------------------------------------


def adding_report(report = "T"):
    total = 0
    numbers = "\nItems\n"
    print = input("Enter an integer, or 'Q'' to quit: ")
    while True: 
        int_or_q = input("Enter an integer, or 'Q' to quit: ")
        if int_or_q.isdigit():
            total += int(int_or_q) 
            if report.startswith("A"):
                numbers += int_or_q + "\n" 
        elif int_or_q.startswith("Q"):
            if report.startwith("A"):
                print(numbers + "\nTotal\n" + str(total))
            else:
                print("\nTotal\n" + str(total)) 
            break    
        if report.startswith("T"):  
            print("\nTotal\n" + str(total))       
        else:
            print(int_or_q, "is an invalid input, try again!")
            break
    adding_report(int_or_q)
    print()


I can't figure out what is wrong with my program, whenever I try to run it nothing happens -- I don't even receive a message about a specific error. Any guidance would be greatly appreciated, thank you!
Reply
#2
you define function adding_report, but after that you never call it. you need to call it with either 'A' or 'T' argument
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Do I need to include "A" in defining the function at the beginning? And I tried using "return" after defining the function but it didn't do anything, when would I call it in the code?
Thanks!
Reply
#4
def adding_report(report = "T"):
    total = 0
    numbers = "\nItems\n"
    print = input("Enter an integer, or 'Q'' to quit: ")
    while True: 
        int_or_q = input("Enter an integer, or 'Q' to quit: ")
        if int_or_q.isdigit():
            total += int(int_or_q) 
            if report.startswith("A"):
                numbers += int_or_q + "\n" 
        elif int_or_q.startswith("Q"):
            if report.startwith("A"):
                print(numbers + "\nTotal\n" + str(total))
            else:
                print("\nTotal\n" + str(total)) 
            break    
        if report.startswith("T"):  
            print("\nTotal\n" + str(total))       
        else:
            print(int_or_q, "is an invalid input, try again!")
            break
    adding_report(int_or_q)
    print()
adding_report('A')
adding_report('T')
note that I didn't look into your code (i.e. if it is correct and do what it is supposed to do)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
That does get it to run, thank you! Except now I get the error "str object not callable," and I don't know what it's referring to
Reply
#6
Please, always post full Traceback, in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Okay, like this format?

Traceback (most recent call last)
<ipython-input-5-49b542a08dca>, line 20, in <module>()
TypeError: 'str' object is not callable
Reply
#8
That would be because you reassigned print on line four. You should never use Python key words for variable names.

Note that you don't use the result of line four anywhere. You could probably just delete it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Thank you! Deleting that line definitely helped it run, it now gives me the input prompt and I'm able to get the integers in. I still get this issue though whenever I try to quit by entering 'Q'

Traceback (most recent call last)
<ipython-input-3-14a13369bd6e>, line 13, in adding_report(report)
AttributeError: 'str' object has no attribute 'startwith'
Reply
#10
(Jun-17-2018, 09:00 PM)sarah_mb_sues Wrote: AttributeError: 'str' object has no attribute 'startwith'
startswith
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python trouble #1 Cicada3301 5 2,132 Nov-17-2021, 09:35 PM
Last Post: Cicada3301
Smile Final Projet - Credit Risk Rauchvant 3 2,438 Nov-18-2020, 03:21 PM
Last Post: Rauchvant
  Help needed, stuck at the final output extricate 0 1,540 Jun-20-2020, 05:35 AM
Last Post: extricate
  Final Project (Databases, GUI, and Classes) poochenthecreator 1 1,646 Apr-27-2020, 09:58 PM
Last Post: deanhystad
  Final Project Help hyg71886 6 3,958 Feb-07-2019, 01:30 AM
Last Post: micseydel
  Final problem Truman 4 4,021 Jan-22-2018, 11:42 PM
Last Post: Truman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020