0

I want to write a program that calculates the GPA of given number of subjects. First the program will prompt the user to enter the number of subjects and then ask "What is the name of class 1?" for the first class and for the second class, it will ask "What is the name of class 2?"

Here, I want to use loop to change the number from 1 to number of classes the user enter. But I cannot increase the number in a 'input' statement.

def main():

names= []
grades = []

number_classes = int(input("How many classes did you take? "))
print()

for i in range(1, number_classes + 1):
    p = input("What is the name of class 1? ")
    names.append(p)
    q = input("What grade did you get in that class? ")
    grades.append(q)

I have attached my piece of code. It asks "What is the name of the class 1? " each time. How can I increase it 1 to 2 to 3 each time it loops?

Naz Islam
  • 409
  • 1
  • 6
  • 20

2 Answers2

0

just append the i value to the string in the inside of the input

for i in range(1, number_classes + 1):
   p = input("What is the name of class"+(i)+"? ")
Ankanna
  • 737
  • 2
  • 8
  • 21
0
for i in range(1, number_classes + 1):
    p = input("What is the name of class " + i)

if you want to put a variable in your print statement you just use the formula

print("text" + variable)

By the way, as a general comment about the way your program is going, you would be much better instead of using two lists, using a list of tuples. A tuple can hold two (or more) different types of data.

For example your tuple could hold two strings(class, grade)

All you would have to do to implement it is something like this

classes = []

number_classes = int(input("How many classes did you take? "))
print()

for i in range(1, number_classes + 1):
    p = input("What is the name of class " + i)
    q = input("What grade did you get in that class? ")
    classes.append((p,q))
Keatinge
  • 4,330
  • 6
  • 25
  • 44
  • Thank you for your helpful information. The code works, but before running it, I had to turn i into a string, as integer cannot be added to a string. Thanks for your help. – Naz Islam Mar 27 '16 at 21:42