1

I think I'm almost there with my assignment. I'm trying to print: "Hi John,(new line) You have a 89.6% in your principle of programming course." However, when I print the below, it shows as follows: "Hi John,(new line) You have a 89.4 % in your principle of programming course."
So could you guys help me how to put % sign without space? Are there better way to print these words?

student_name = input("Enter student's name: ")
course_name = input("Enter course name: ")

quizzes_weight = input("Enter quizzes weight: ")
projects_weight = input("Enter projects weight: ")
activities_weight = input("Enter activities weight: ")
attendance_weight = input("Enter attendance weight: ")
exams_weight = input("Enter exams weight: ")

quizzes_average = input("Enter quizzes average: ")
projects_average = input("Enter projects average: ")
activities_average = input("Enter activities average: ")
attendance_average = input("Enter attendance average: ")
exams_average = input("Enter exams average: ")

quizzes_weight = float(quizzes_weight)
projects_weight = float(projects_weight)
activities_weight = float(activities_weight)
attendance_weight = float(attendance_weight)
exams_weight = float(exams_weight)

quizzes_average = float(quizzes_average)
projects_average = float(projects_average)
activities_average = float(projects_average)
attendance_average = float(attendance_average)
exams_average = float(exams_average)

average_score_for_course = ((quizzes_weight * quizzes_average) + 
(projects_weight * projects_average) + (activities_weight * 
activities_average) + (attendance_weight * attendance_average) + 
(exams_weight * exams_average)) * 100

print("Hi",student_name +",","\nYou have a", 
average_score_for_course,"% in your", course_name,"course.")

for this assignment my above programming should produce output as below according to my input coding. For example:

Enter student's name: Ryan
Enter course name: Advanced Quantum Mechanics

Enter quizzes weight: .1
Enter projects weight: .2
Enter activities weight: .3
Enter attendance weight: .1
Enter exams weight: .3

Enter quizzes average: 1
Enter projects average: .85
Enter activities average: .9
Enter attendance average: .95
Enter exams average: .87

the output should be something like this:

Hi Ryan,
You have a 89.6% in your Advanced Quantum Mechanics course.
Ji. K
  • 47
  • 1
  • 7

4 Answers4

0

You can format a string with the 3 variables you want to print:

print("Hi {},\nYou have a {}% in your {} course".format(student_name, average_score_for_course, course_name))
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Danielle M.
  • 3,607
  • 1
  • 14
  • 31
0

Looking at your code:

print("Hi",student_name +",","\nYou have a", 
average_score_for_course,"% in your", course_name,"course.")

When you use a comma delimiter for the print function, it adds a space by default. Replace your comma with a +:

print(average_score_for_course + "% in your")

By replacing the comma with +, it removes the space that Python adds by default.

Using Emma's solution (string formatting) is a better and more pythonic solution to your problem. Hopefully I helped you to understand why this is happening, though.

Brice Frisco
  • 409
  • 1
  • 4
  • 15
0

Just for a different answer: You can try this as well

average_score_for_course = str(((quizzes_weight * quizzes_average) + 
(projects_weight * projects_average) + (activities_weight * 
activities_average) + (attendance_weight * attendance_average) + 
(exams_weight * exams_average)) * 100)+"%"

print("Hi",student_name +",","\nYou have a", 
average_score_for_course,"in your", course_name,"course.")

change the result to string and concatenate "%" to it.

0
print("Hi" + str(student_name) +"," + "\nYou have a" + str(average_score_for_course) +"% in your" + str(course_name) + "course.")

For me i will try to avoid using comma because it hard to visualize how your printing looks like and for safe it is better to convert "average_score_for_course" into str before printing.

WenJuan
  • 624
  • 1
  • 8
  • 21