Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 31st, 2012, 04:24 AM
 zSnipeAce
Registered User

Join Date: Oct 2012
Posts: 1
Time spent in forums: 1 h 5 m 25 sec
Reputation Power: 0
Python 3 HELP

Hey guys, I am new to the thread(clearly), and I am new to programming altogether. I can't seem to figure out how to make this program and I have tried and tried different ways along with many Google/YouTube searches before coming here.

The objective of this assignment is to demonstrate your understanding of functions and a menu controlled interface by using them in your solution to this programming assignment.

Instructions:

Create a program with three functions and call each of the functions within a while loop.

The first function takes no parameters, nor does it return a value. It simply displays a menu of selections.

The second function takes one parameter and returns a value. The parameter is a numeric grade, e.g. 88. The value returned is the associated letter grade as determined by the University grade scale.

The third function takes two parameters and computes a value based on the values of its parameters and returns the result. The first parameter is the numeric grade and the second is the associated letter grade. The value returned is the number of extra credit points, in 1/2 point increments, needed to raise the letter grade one small step, # e.g. from C to C+.

The program should remember and display the numeric grade entered by the user immediately after the menu is displayed. The menu should be displayed for each pass through the while loop.

This is what I have so far:

#
import random
import time

if percentage >= 95:
elif percentage >= 92:
elif percentage >= 89:
elif percentage >= 86:
elif percentage >= 83:
elif percentage >= 80:
elif percentage >= 77:
elif percentage >= 74:
elif percentage >= 71:
elif percentage >= 68:
elif percentage >= 65:
else:
percentage <= 65

print('1: Enter grade in percentage (0-100)')
print('3: Calculate letter grade with extra credit')
print('4: Calculate exctra credit needed to boost letter grade')
print('0: Exit')
print()

loop = 1
choice = 0
while loop == 1:
if choice == 1:
elif choice ==2:
elif choice ==3:
print(percentage + (input))
elif choice ==4:
print()
elif choice ==0:
loop = 0

#####
output I'm looking for:

1: Enter grade in percentage (0-100)
3: Calculate letter grade with extra credit
4: Calculate extra credit needed to boost letter grade
0: Exit

1: Enter grade in percentage (0-100)
3: Calculate letter grade with extra credit
4: Calculate extra credit needed to boost letter grade
0: Exit

1: Enter grade in percentage (0-100)
3: Calculate letter grade with extra credit
4: Calculate extra credit needed to boost letter grade
0: Exit

Enter extra credit points: 3
If you can earn 3.0 points extra credit your letter grade would be: C

1: Enter grade in percentage (0-100)
3: Calculate letter grade with extra credit
4: Calculate extra credit needed to boost letter grade
0: Exit

In order to boost your grade from C- to C, 2.0 points extra credit are needed.

1: Enter grade in percentage (0-100)
3: Calculate letter grade with extra credit
4: Calculate extra credit needed to boost letter grade
0: Exit

Done!

#2
November 1st, 2012, 09:47 AM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,798
Time spent in forums: 1 Month 2 Weeks 3 Days 1 h 39 m 19 sec
Reputation Power: 1569
For future reference, you should know that the forum software does not by default retain indentation. In order to show the code in a readable and meaningful manner, you need to use either [code] or [highlight] tags around the code sample. There are buttons at the top of the editing window that you can use to do this automatically, as well - the hash (#) button is for plain code samples, the highlight marker button for larger code samples. Since you are new here, I have done this for you to show you the difference:

Python Code:
 Original - Python Code
``` #import randomimport time def get_letter_grade(percentage):    if percentage >= 95:        letter_grade = "A"    elif percentage >= 92:        letter_grade = "A-"    elif percentage >= 89:        letter_grade = "B+"    elif percentage >= 86:        letter_grade = "B"    elif percentage >= 83:        letter_grade = "B-"    elif percentage >= 80:        letter_grade = "C+"    elif percentage >= 77:        letter_grade = "C"    elif percentage >= 74:        letter_grade = "C-"    elif percentage >= 71:        letter_grade = "D+"    elif percentage >= 68:        letter_grade = "D"    elif percentage >= 65:        letter_grade = "D-"    else:        percentage <= 65        letter_grade = "F"        def menu_choice():    print('1: Enter grade in percentage (0-100)')    print('2: Get letter grade from percentage grade')    print('3: Calculate letter grade with extra credit')    print('4: Calculate extra credit needed to boost letter grade')    print('0: Exit')    print()    return input ("Please make your choice:")        loop = 1choice = 0percentage = 0while loop == 1:    choice = menu_choice()    if choice == 1:        print("Enter percentage grade(0-100)?")    elif choice ==2:        print(letter_grade)    elif choice ==3:        print(percentage + (input))    elif choice ==4:        print()    elif choice ==0:        loop = 0
```

That having been said, can you tell us where you are having trouble with it?

I can see at least one major problem right off the bat - in the return value for menu_choice(), you are returning a string, not an integer. You can convert the input value to an integer, but that could raise an exception; it may be easier just to have choice as a string, and compare it to strings:
Code:
```loop = 1
choice = ''
percentage = 0
while loop == 1:
if choice == '1':
elif choice =='2':```

Note that I also added initializers for percentage and letter_grade, to make sure that they are visible outside of the loop.

Next, you never read in a value for percentage under option 1. You need to use the input() function to get the value you want; but that isn't quite enough by itself, as you'll also need to convert the result to an integer, and catch any user mistakes that crop up. That means you need what is called an exception frame around your int() cast:
Code:
```    if choice == '1':
try:
except:

Also, you are treating the variable letter_grade as if it were a global, but as it is written here it is actually two different variables in different scopes. What you probably want to do is return the value of letter_grade at the end of get_letter_grade(), like so:
Code:
`    return letter_grade`

Then, you need to call get_letter_grade() in your main program, and assign the local variable letter_grade the returned value:
Code:
```    elif choice =='2':

Anything else?

Oh, I might add that there are more Pythonic ways to write get_letter_grade(), ones which would simplify writing the fourth function as well. I would recommend using a dictionary object, if you have been shown how to use those yet.
zSnipeAce agrees!
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : November 1st, 2012 at 08:43 PM.

 Viewing: Dev Shed Forums > Other > Beginner Programming > Python 3 HELP

## Developer Shed Advertisers and Affiliates

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |