Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Just a beginner at Python trying to make a GPA calculator as a basic project, but getting an error in syntax: "expected an indented block"
import keyword
classes = int(input("How many classes do you need to calculate?: "))
gpa = 0.0
if classes == 1:
credits = int(input("Please enter the amount of credit hours of the class: "))
grade = input("What was the grade you have earned?: ")
letter_points = letterGradeConversion()
print(letter_points)
elif classes == 2:
credits = int(input("Please enter the amount of credit hours of the first class: "))
grade = input("What was the grade you have earned?: ")
credits = int(input("Please enter the amount of credit hours of the second class: "))
grade = input("What was the grade you have earned?: ")
elif classes == 3:
credits = int(input("Please enter the amount of credit hours of the first class: "))
grade = input("What was the grade you have earned?: ")
credits = int(input("Please enter the amount of credit hours of the second class: "))
grade = input("What was the grade you have earned?: ")
credits = int(input("Please enter the amount of credit hours of the third class: "))
grade = input("What was the grade you have earned?: ")
elif classes == 4:
credits = int(input("Please enter the amount of credit hours of the first class: "))
grade = input("What was the grade you have earned?: ")
credits = int(input("Please enter the amount of credit hours of the second class: "))
grade = input("What was the grade you have earned?: ")
credits = int(input("Please enter the amount of credit hours of the third class: "))
grade = input("What was the grade you have earned?: ")
credits = int(input("Please enter the amount of credit hours of the fourth class: "))
grade = input("What was the grade you have earned?: ")
else:
print("The max number of classes on this application is currently 4")
def letterGradeConversion():
if grade == A:
gpa = gpa + 4
elif grade == B:
gpa = gpa + 3
elif grade == C:
gpa = gpa + 2
elif grade == D:
gpa = gpa + 1
elif grade == F:
gpa = gpa + 0
else:
print("That is not a valid letter grade")
Not in code:
iuashd;alksdjas;kjdha;slkjaskljsakdljas;ljlkjf;lkjsFLKAJSD;KJASD;KLk;jaS'LKDJ;ldjk;'DH'AHKL'ASJKLASJKQIOWJWQOIH
Your function definition (everything after def ... :) has to be indented.
So
def letterGradeConversion():
if grade == A:
gpa = gpa + 4
...
instead of
def letterGradeConversion():
if grade == A:
gpa = gpa + 4
...
The entire block function definition for letterGradeConversion needs to be indented. Take a look at Python docs on indentation - https://docs.python.org/2.3/ref/indentation.html
Related
Create a program that will calculate a student’s grade in a class that uses a weighted grade scale.
I have to be able to manually enter each Quiz, Test, Participation, and Project Grade. It's my first year learning Python at all, and it is my first language! Thanks for the help!
Edit: Right now the code is giving me this:
"
John Doe
John Doe , You made a function letter_grade at 0x7f4424330dd0"
name="John Doe"
print(name)
#Entering Particiaption Grade and Weight
def Participation(Participation1):
Participation1= int(input())
participation_average=int(input(Participation1/1))
return participation_average
def ParticipationW(ParticipationWeight):
ParticipationWeight = int(input())
return ParticipationWeight
#Entering Quiz Grades and Weight
def Quiz(Quiz1, Quiz2, Quiz3, Quiz4, Quiz5, Quiz6):
Quiz1 = int(input())
Quiz2 = int(input())
Quiz3 = int(input())
Quiz4 = int(input())
Quiz5 = int(input())
Quiz6 = int(input())
quiz_average=int(input((Quiz1+Quiz2+Quiz3+Quiz4+Quiz5+Quiz6)/6))
return quiz_average
def QuizW(QuizWeight):
QuizWeight = int(input())
return QuizWeight
#Projects
def Project(Project1, Project2, Project3, Project4, Project5, Project6):
Project1 = int(input())
Project2 = int(input())
Project3 = int(input())
Project4 = int(input())
Project5 = int(input())
Project6 = int(input())
project_average=int(input((Project1+Project2+Project3+Project4+Project5+Project6)/6))
return project_average
def ProjectW(ProjectWeight):
ProjectWeight = int(input())
return ProjectWeight
#Enter Test Grades and Variables
def Test(Test1, Test2, Test3, Test4):
Test1 = int(input())
Test2 = int(input())
Test3 = int(input())
Test4 = int(input())
test_average=int(input((Test1+Test2+Test3+Test4)/4))
return test_average
def TestW(TestWeight):
TestWeight = int(input())
return TestWeight
def letter_grade(FinalGrade):
FinalGrade=int(input((Participation * ParticipationW) + (Quiz * QuizW) + (Project * ProjectW) + (Test* TestW)))
if FinalGrade >= 90:
return "You made a A!"
elif FinalGrade >= 80:
return "You made a B!"
elif FinalGrade >= 70:
return "You made a C!"
elif FinalGrade >= 60:
return "You made a D!"
else:
return "Sorry, you failed."
print(name, ", You made a", letter_grade, ".")
This is what he has given us so far:
Enter Student Name:
Please enter the total possible quiz grade:60
Please enter quiz weight in decimal form:.15
Please enter the first quiz grade:10
Please enter the second quiz grade:5
Please enter the third quiz grade:6
Please enter the fourth quiz grade:7
Please enter the fifth quiz grade:10
Please enter the sixth quiz grade:9
Please enter the total possible Participation grade:100
Please enter participation weight in decimal form:.05
Please enter the Participation grade:100
Please enter the total possible project grade:600
Please enter project weight in decimal form:.50
Please enter the first project grade:100
Please enter the second project grade:100
Please enter the third project grade:90
Please enter the fourth project grade:80
Please enter the fifth project grade:80
Please enter the sixth project grade:50
Please enter the total possible test grade:400
Please enter test weight in decimal form:.30
Please enter the first test grade:100
Please enter the second test grade:90
Please enter the third test grade:80
Please enter the fourth test grade:50
Jon Doe's grades are:
Overall Quiz Grade: 11.75
Overall Participation Grade: 5.0
Overall Project Grade: 41.66666666666667
Overall Test Grade: 24.0
Final Grade: 82.41666666666667
Your Grade: B
Here is your answer dear all the best and welcome to Python
Code
name=input("Enter your name:")
def grade():
#Entering Particiaption Grade and Weight
Participation1= int(input("Entering Particiaption Grade :"))
participation_average=Participation1
ParticipationWeight = int(input("Entering Particiaption Weight:"))
#Entering Quiz Grades and Weight
Quiz1 = int(input("Enter Quiz 1 Grades:"))
Quiz2 = int(input("Enter Quiz 2 Grades:"))
Quiz3 = int(input("Enter Quiz 3 Grades:"))
Quiz4 = int(input("Enter Quiz 4 Grades:"))
Quiz5 = int(input("Enter Quiz 5 Grades:"))
Quiz6 = int(input("Enter Quiz 6 Grades:"))
quiz_average=(Quiz1+Quiz2+Quiz3+Quiz4+Quiz5+Quiz6)/6
QuizWeight = int(input("Enter Quiz weight:"))
#Projects
Project1 = int(input("Enter Project 1 Grades:"))
Project2 = int(input("Enter Project 2 Grades:"))
Project3 = int(input("Enter Project 3 Grades:"))
Project4 = int(input("Enter Project 4 Grades:"))
Project5 = int(input("Enter Project 5 Grades:"))
Project6 = int(input("Enter Project 6 Grades:"))
project_average=(Project1+Project2+Project3+Project4+Project5+Project6)/6
ProjectWeight = int(input("Enter Project Weight:"))
#Enter Test Grades and Variables
Test1 = int(input("Enter Test 1 Grades:"))
Test2 = int(input("Enter Test 2 Grades:"))
Test3 = int(input("Enter Test 3 Grades:"))
Test4 = int(input("Enter Test 4 Grades:"))
test_average=(Test1+Test2+Test3+Test4)/4
TestWeight = int(input("Enter Test Weight:"))
FinalGrade=(participation_average* ParticipationWeight) + (quiz_average* QuizWeight) + ( project_average * ProjectWeight) + (test_average* TestWeight)
print("Dear ",name,",")
if FinalGrade >= 90:
print("You made a A!")
elif FinalGrade >= 80:
print( "You made a B!")
elif FinalGrade >= 70:
print("You made a C!")
elif FinalGrade >= 60:
print("You made a D!")
else:
print("Sorry, you failed.")
grade()
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
if grade >= 3:
print "You can play this game."
else:
print "Sorry, you can't play the game."
if age is true and grade is false,this program prints wrong output.but if age is false, it prints correct output.
Why is it happening?
You are leaving open the possibility that age >= 8 but grade < 3 in which you have no control flow to handle. You can correct this succinctly with an and statement
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8 and grade >= 3:
print "You can play this game."
else:
print "Sorry, you can't play the game."
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
if grade >= 3:
print"You can play this game."
else:
print"Sorry , you can't play the game."
else:
print "Sorry , you can't play the game."
You have to include an 'else' condition in your nested if/else statement:
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
if grade >= 3:
print "You can play this game."
else:
print"Sorry, you can't play this game."
else:
print "Sorry, you can't play this game."
I'm working on a homework assignment for my game dev. class and I can't quite figure out what I'm doing wrong. I am trying to add numbers to a high score that has already been hardwired in by the use of user input. For example; Trying to add 100 points to Roger who currently has 3456 points. I just haven't quite figured out what I'm doing wrong to get the code to work. All and any help is very appreciated. Thank you.
str1 = ["Roger", 3456]
str2 = ["Justin", 2320]
str3 = ["Beth", 1422]
enter = 0
start = 0
Roger = ()
def incdec(sco):
return addit
def addition(num1):
return num1 + num1
def square(num):
print("I'm in square")
return num * num
def display(message):
"""Display game instuctions"""
print(message)
def instructions():
"""Display game instuctions"""
print("Welcome to the world's greatest game")
def main():
instructions()
scores = [str1, str2, str3]
start = input("Would you like to view the high score options? y/n ")
if start == "y":
print("""\
Hello! Welcome to the high scores!
Here are the current high score leaders!:
""")
print(scores)
print("""\n\
0 - Sort high scores
1 - Add high score
2 - Reverse the order
3 - Remove a score
4 - Square a number
5 - Add 2 numbers together
6 - Add to a score
7 - Subtract from a score
""")
option = int(input("Please enter your selection "))
while option < 8:
print(scores)
if option == 0:
scores.sort()
print("These are the scores sorted alphabetically")
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 1:
print(scores)
print("Please enter your name and score; After entering your name, hit the return key and enter your score")
name = input()
score = int(input())
entry = (name,score)
scores.append(entry)
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 2:
print(scores)
scores.reverse()
print("\nHere are the scores reversed")
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 3:
print(scores)
print("Please enter the high score you would like to remove. After typing the name, hit the return key and enter the score")
name1 = input()
score1 = int(input())
remove = (name1,score1)
scores.remove(remove)
print(scores)
option = option = int(input("Please enter your selection"))
elif option == 4:
val = int(input("Give me a number to square"))
sqd = square(val)
print(sqd)
option = option = int(input("Please enter your selection"))
elif option == 5:
val0 = int(input("Give me one number"))
val1 = int(input("Give me another number"))
addi = (val0 + val1)
print(addi)
option = option = int(input("Please enter your selection"))
elif option == 6:
sc0 = input("Please enter the person whose score you would like to add to ")
sc1 = int(input("Please enter the amount you would like to add to their score "))
addit =(sc0 + str(sc1))
print(addit)
option = option = int(input("Please enter your selection"))
elif option == 7:
break
main()
You're adding a number to a string. What you need to do is search for the player in your scores list and then add the given number to their current score. Using your current structure, you could do the following:
def update_score(scores, person, amount):
# Loop through the current scores
for score in scores:
# Find the person we're looking for
if score[0].lower() == person.lower():
# Update their score
score[1] += amount
return scores
And then modify as such:
...
elif option == 6:
sc0 = input("Please enter the person whose score you would like to add to ")
sc1 = int(input("Please enter the amount you would like to add to their score "))
scores = update_score(scores, sc0, sc1)
print(scores)
option = option = int(input("Please enter your selection"))
...
Which produced for me:
Please enter the person whose score you would like to add to roger
Please enter the amount you would like to add to their score 100
[['Roger', 3556], ['Justin', 2320], ['Beth', 1422]]
However, I would refactor a lot of the boilerplate away and use dictionary lookups for tracking players. But the above solution solves your stated problem.
Im new to python and I am currently working on a python script and want to add a loop at the end of it, currently the code is as follows:
#FinalGrade
print ("\n")
Institution = str(input("Please Enter the Name of Your insitution: "))
print ("\n")
Year = str(input("Please Enter the Year of the Student (For Example, 'Year 2'): "))
print ("\n")
Student = str(input("Student Full Name: "))
print ("\n")
Grade1 = int(input("Enter Student's First Term Grade: "))
Grade2 = int(input("Enter Student's Second Term Grade: "))
Grade3 = int(input("Enter Student's Third Term Grade: "))
Grade4 = int(input("Enter Student's Fourth Term Grade: "))
average = (Grade1+Grade2+Grade3+Grade4)/4
print ("\n")
print ("Total Grade Average: %G" % (average))
passed_or_failed = "PASSED"
if average < 40:
passed_or_failed = 'FAILED'
print ("\n")
print ("%s has: %s" % (Student, passed_or_failed))
Id like to find out if it would be possible to set a loop so another student can be entered, would this be possible?
Thank you
Why not put it in an infinite loop?
cont = 'y'
while cont=='y':
print ("\n")
Institution = str(input("Please Enter the Name of Your insitution: "))
print ("\n")
Year = str(input("Please Enter the Year of the Student (For Example, 'Year 2'): "))
print ("\n")
Student = str(input("Student Full Name: "))
print ("\n")
Grade1 = int(input("Enter Student's First Term Grade: "))
Grade2 = int(input("Enter Student's Second Term Grade: "))
Grade3 = int(input("Enter Student's Third Term Grade: "))
Grade4 = int(input("Enter Student's Fourth Term Grade: "))
average = (Grade1+Grade2+Grade3+Grade4)/4
...
cont = input('Do you want to keep entering students? y/n: ')
Or if you want to keep all of the results:
results = []
cont = 'y'
while cont=='y':
print ("\n")
Institution = str(input("Please Enter the Name of Your insitution: "))
...
passed_or_failed = "PASSED"
if average < 40:
passed_or_failed = 'FAILED'
results.append(passed_or_failed)
...
cont = input('Do you want to keep entering students? y/n: ')
And you can just loop through the results to see them.
You mean just one more student to be entered?
You can probably do that with a while or for loop. Something along these lines:
counter = 0
while (counter < 2):
your existing code
....
counter += 1
I have been trying to get this program to work, and it runs but once I got it to run I uncovered this problem. Right now it asks for gradePoint and credit hours and prints out a GPA like it is supposed to, but I can type anything in gradePoint and it will spit out a value. What I want is gradePoint to only work if the user types in a number between 0.0-4.0.
I was thinking of a loop of some sort, like an if loop but is there an easier way (or a preferred way) to do this?
Here's the code:
def stu():
stu = Student
class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQpoints(self):
return self.qpoints
def getStudent(infoStr):
name, hours, qpoints = infoStr.split("\t")
return Student(name, hours, qpoints)
def addGrade(self, gradePoint, credits):
self.qpoints = gradePoint * credits
self.hours = credits
self.gradePoint = float(gradePoint)
self.credits = float(credits)
def gpa(self):
return self.qpoints/self.hours
def main():
stu = Student(" ", 0.0, 0.0)
credits = int(input("Enter Credit Hours "))
gradePoint = float(input("Enter Grade Point"))
if gradePoint > 4.0:
print("Please enter a Grade Point that is less then 4.")
stu.addGrade(gradePoint,credits)
output = stu.gpa()
print("\nStudent GPA is: ", output)
main()
This is the shell printout(I know the grade point is repeated in the GPA, this is for a class and we were supposed to use a skeleton program and change some things):
Enter Credit Hours 4
Enter Grade Point 3.5
Student GPA is: 3.5
Enter Credit Hours 12
Enter Grade Point 3.2
Student GPA is: 3.2
Thanks!
EDIT: I just did an if loop as suggested in the comments and it worked somewhat in that it printed out what I wanted but didn't make it stop and wait for the correct input. I'm not exactly sure how to get it to do that.
Here is the new print from the shell:
Enter Credit Hours 4
Enter Grade Point 3.5
Student GPA is: 3.5
Enter Credit Hours 4
Enter Grade Point 4.5
Please enter a Grade Point that is less then 4.
Student GPA is: 4.5
You can check if user input meet your requirements and throw an exception if not. This will make the program log an error message and stop on invalid user input.
The most basic way to do this is to use an assert statement:
def main():
stu = Student(" ", 0.0, 0.0)
credits = int(input("Enter Credit Hours "))
assert credits > 0 and credits < 4, "Please enter a number of Credit Hours between 0 and 4."
If you wish the application to keep prompting the user until a valid input is entered, you will need at least one while loop.
For exemple:
def main():
stu = Student(" ", 0.0, 0.0)
while True:
credits = int(input("Enter Credit Hours "))
if credits > 0 and credits < 4:
break
else:
print("Please enter a number of Credit Hours between 0 and 4")
while True:
gradePoint = float(input("Enter Grade Point"))
if gradePoint <= 4.0:
break
else:
print("Please enter a Grade Point that is less then 4.")
stu.addGrade(gradePoint,credits)
output = stu.gpa()
print("\nStudent GPA is: ", output)
Create a function that gathers a valid float from the user within a range:
def getValidFloat(prompt, error_prompt, minVal, maxVal):
while True:
userInput = raw_input(prompt)
try:
userInput = float(userInput)
if minVal <= userInput <= maxVal:
return userInput
else: raise ValueError
except ValueError:
print error_prompt
And then use like this:
gpa = getValidFloat(
"Enter Grade Point ",
"You must enter a valid number between 1.0 and 4.0",
1,
4)
The output would look something like this:
Enter Grade Point 6.3
You must enter a valid number between 1.0 and 4.0
Enter Grade Point 3.8