grand_78

Okay so my code is almost complete but I still need one small thing that I can figure out. When running my code it will add 5 when accelerating and subtract 5 when braking, but I need it to display an error when speed >0 when braking, then reset speed back to 0. I know this needs to be done in my def for brake but I can't figure out the proper line of code. Here is my code, thanks for any help. [(http://stackoverflow.com/questions/37124167/need-fixes-for-a-few-errors-python?noredirect=1#comment61836425_37124167)]

grand_78

Okay so my code works, but I need a few more requirements for it and I can't seem to get them right even though they should be pretty simple. I just need the window to display an error message when the values entered are not numerical and when the values entered are less than 0. For example when # of gallons used is -12 I need it to display an error message below the buttons, and same when letters are entered instead of numbers. I also need the calculated mpg to display to 2 decimal places and keep getting errors for that as well. Here is my code, thanks for any help.

import tkinter
class MPG: 
    def __init__(self):
    #create window
    self.main_window = tkinter.Tk()

    #create 4 frames
    self.gallons_frame = tkinter.Frame(self.main_window)
    self.miles_frame = tkinter.Frame(self.main_window)
    self.mpg_frame = tkinter.Frame(self.main_window)
    self.button_frame = tkinter.Frame(self.main_window)

    self.gallons_label = tkinter.Label(self.gallons_frame, \
                                    text= "Enter the number of gallons:")
    self.gallons_entry = tkinter.Entry(self.gallons_frame, \
                                        width=10)
    self.gallons_label.pack(side="left")
    self.gallons_entry.pack(side="left")
    self.miles_label = tkinter.Label(self.miles_frame, \
                                    text= "Enter the number of miles:")
    self.miles_entry = tkinter.Entry(self.miles_frame, \
                                        width=10)
    self.miles_label.pack(side="left")
    self.miles_entry.pack(side="left")
    self.result_label = tkinter.Label(self.mpg_frame, \
                                    text="Miles Per Gallon")
    self.mpg = tkinter.StringVar()
    self.mpg_label = tkinter.Label(self.mpg_frame, \
                                    text variable=self.mpg)
    self.result_label.pack(side="left")
    self.mpg_label.pack(side="left")
    self.calc_button = tkinter.Button(self.button_frame, \
                                        text="Calculate MPG" , \
                                        command=self.calc_mpg)
    self.quit_button = tkinter.Button(self.button_frame, \
                                        text="Quit", \
                                        command=self.main_window.destroy)
    self.calc_button.pack(side="left")
    self.quit_button.pack(side="left")

    self.gallons_frame.pack()
    self.miles_frame.pack()
    self.mpg_frame.pack()
    self.button_frame.pack()
#the calc_mpg is the callback function for the calc_button
def calc_mpg(self):
#Get miles and gallons and store them in variables
    self.gallons = float(self.gallons_entry.get())

    self.miles = float(self.miles_entry.get())

#calculate the MPG
    self.miles_per_gallon = (self.miles / self.gallons)

#update mpg_label
    self.mpg.set(str(round(self.miles_per_gallon)))

carmpg ...

grand_78

@Freshly I put the line you suggested and now it does put the 45 on the same line but it also repeats the other names. Please send file to me?

grand_78

@Freshly I'm about to give up, I literally typed this into my code and it's still not working. Now it only displays the first name in the list. I dont understand what's wrong. Nothing I put seems to work, so frustrating.

grand_78

@Freshly is something wrong with my if statement in my code? The "F" keeps displaying itself on a new line below the 45. I appreciate you trying to help me.

grand_78

I need help with just one line. I need an "F" to be displayed on the same line the student with 45 points. I keep getting the "F" to display on a new line below the 45. I guess something is wrong with my if statement. I also need to display the % symbol after the 83.3. I'm using python 3.5.1. Thank you so much for any help.

def main():
    file_students = open("student_points.txt" , "r")
    stu_name = file_students.readline()

    num_stu = 0
    f_students = 1
    pass_students = 5/6

    print("Student\t\t\tPoints\t\tGrade")
    print("-------------------------------------\n")
    while stu_name != "":
        stu_name = stu_name.rstrip("\n")
        stu_points = file_students.readline()
        stu_points = int(stu_points)
        print(stu_name, "\t\t", stu_points, sep="")
        num_stu += 1

        if stu_points < 60:
            print("\t\t\t\t\tF")

        stu_name = file_students.readline()

    file_students.close()
    print()
    print("Number of students processed=", num_stu)
    print("% of students who passed=" , format(pass_students * 100 , ".1f"))

main()

The students and their corresponding grades are:
Johnson Smith: 93
Maryanne James: 80
Stanton Chase: 45
Mildred Morris: 90
George Deitz: 89
Maisie Kling: 79

grand_78

@Gribouillis I keep getting the "F" to go on a separate line, do you know how I could get the F to display on the same line as "45"? Thank you

grand_78

@Gribouillis I keep getting invalid syntax for putting

if stu_points is 60>:
    print("F")

I don't understand what is invalid

grand_78

Hi, I'm working on this problem in my python programming class and need help with part of it. I need to have it display a "F" for anyones grade that is below 60, and also have it display the amount of "F" students, as well as the percentage of students who passed. My code correctly displays the points earned by each student but i can't figure out the rest.
The students and their corresponding grades are:
Johnson Smith: 93
Maryanne James: 80
Stanton Chase: 45
Mildred Morris: 90
George Deitz: 89
Maisie Kling: 79
My code is as follows:

def main():
    file_students = open("student_points.txt" , "r")
    stu_name = file_students.readline() #read the first record
                                        #which is the first student's name
    num_stu = 0

    print("Student\t\t\tPoints\t\tGrade")
    print("--------------------------------\n")
    while stu_name != "":
        stu_name = stu_name.rstrip("\n")    #strip \n from name
        stu_points = file_students.readline() #read points
        stu_points = int(stu_points)     #cast points into an int

        print(stu_name, "\t\t", stu_points, sep="")
        num_stu += 1

        stu_name = file_students.readline()

    file_students.close()
    print()
    print("Number of students processed =", num_stu)

main()

Thanks for any help

grand_78

I am a beginner in computer programming and need help with this problem: Create a script that prompts the user to provide the number of pennies, nickels, dimes, and quarters. Display back the value of the coins in dollars and cents.

Here is my code so far, everything is good except it doesn't properly display the number of cents left over. For example; if the total is $1.76 it will say "you have 1 dollar and 1 cent(s). Thanks so much for any tips/help.

def main():
    pennies = int(input("Enter pennies:"))
    nickels = int(input("Enter nickels:"))
    dimes = int(input("Enter dimes:"))
    quarters = int(input("Enter quarters:"))

    print("You entered:")
    print("\tPennies:" , pennies)
    print("\tNickels:" , nickels)
    print("\tDimes:" , dimes)
    print("\tQuarters:" , quarters)

    total_value = get_total(pennies, nickels, dimes, quarters)
    dollars = get_dollars(pennies, nickels, dimes, quarters)
    left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)

    print("Total =$" , total_value, sep="")
    print("You have", dollars, "dollars and", left_over_cents, "cent(s)")

def get_total(pennies, nickels, dimes, quarters):
    pennies = (.01 * pennies);
    nickels = (.05 * nickels);
    dimes = (.10 * dimes);
    quarters = (.25 * quarters);
    total_value = pennies + nickels + dimes + quarters
    return total_value

def get_dollars(pennies, nickels, dimes, quarters):
    total_value = get_dollars(pennies, nickels, dimes, quarters)
    dollars = format(total_value // 1, ".0f")
    return dollars

def get_left_over_cents(pennies, nickels, dimes, quarters):
    total_value = get_dollars(pennies, nickels, dimes, quarters)
    left_over_cents = total_value
    return left_over_cents

main()

grand_78

Finally figured it out myself, thanks though.

grand_78

Can someone please help me solve this problem? I've tried 20 different things and have had no such luck. The problem is:

The Fast Freight Shipping Company charges the following rates:
2 pounds or less --- $1.50 per pound
Over 2 pounds but not more than 6 pounds---$3.00 per pound
Over 6 pounds but not more than 10 pounds---$4.00 per pound
Over 10 pounds---$4.75 per pound

Write a program that asks the user to enter the weight of a package and then displays the shipping charges formatted to 2 decimal places after the decimal point.

Any help would be much appreciated as this problem has caused lots of frustration.