0

Here is what i've tried to do so far, but i'm having difficulties looping the input and keep getting TypeError: unsupported operand type(s) for +: 'int' and 'str'

def main():
#lists
    courses=[]
    grade=[]

#Entering input
    courses.append(input('Enter course or press Enter to quit '))
    grade.append(int(input('Enter percent achieved ')))

    grades=open("grades.txt",'w')



#loop to write to file
    for element in courses:
        grades.write(element + '\n')

    for element in grade:
        grades.write(element + '\n')





#close file and print

        return
if input==''
print('File was created and closed')  


main()

here is an example of how the program should run

SAMPLE RUN

Enter course or Enter to quit math

Enter percent achieved 88

Enter course or Enter to quit comm

Enter percent achieved 93

Enter course or Enter to quit chem

Enter percent achieved 80

Enter course or Enter to quit ethics

Enter percent achieved 96

Enter course or Enter to quit 

File was created and closed
Vasilis G.
  • 7,174
  • 4
  • 19
  • 28
Sock Monkey
  • 313
  • 1
  • 16
  • You're adding an `int` to `grade` here: `grade.append(int(input('Enter percent achieved ')))` and then trying to "add" this `int` to a string: `for element in grade: grades.write(element + '\n')` – Nir Alfasi Nov 19 '17 at 16:02

1 Answers1

0

As it stands the issue is this line:

grade.append(int(input('Enter percent achieved ')))

The input() returns the userinput as a string, but you turn it into an integer. You can't simply add a string and integer, hence you get the error. Since it looks like you're not actually doing anything with the grade other than writing it to the file, the easiest fix is to remove the int():

grade.append(input('Enter percent achieved '))

That way the script works as intended (one hopes) and the file is written successfully. You'd only need the grade as an integer if you're planning to do other (mathy) operations with it. If you really want to use an integer for any reason, you'll have to turn it back into a string when you write your file, e.g.

grades.write(str(element) + '\n')
# str() to turn the grade into a string, 

EDIT: Looping for multiple inputs:

Two possibilities for looping the input. Both are infinite loops, so the script keeps asking for grades until the user deliberately exits. You could also implement a counter, maximum or use a for loop if you want to restrict the number of iterations. I can add an example if you like, but for now they didn't seem necessary.

Version 1 uses try except. When input is left blank it raises a KeyboardInterrupt, which is caught by the except and used to exit the while loop. This isn't the best idea, since it will catch any KeyboardInterrupt and lets the script continue even if the user is actually trying to stop it.

Version 2 uses a different input to break the loop, avoiding the issue with the KeyboardInterrupt.

# This will loop infinitely until the user exits by pressing enter on a blank input.
# Downside: This catches any KeyboardInterrupt including user's attempts to stop the 
# script.
while True:
    try: 
        courses.append(input('Enter course or press Enter to quit '))
    except KeyboardInterrupt:
        break

    try:
        grade.append(int(input('Enter percent achieved ')))
    except KeyboardInterrupt:
        break

    # Alternative: User enters x (or any other string you choose) to exit. 
while True:

    course = input('Enter course or enter x to quit ')
    grade =  int(input('Enter percent achieved '))

    if course.strip() == 'x' or grade.strip() == 'x':
        break
    # Exit the loop if x was entered. The strip removes any unwanted whitespace.
    # e.g. '   x' will still be recognised. ('X' will not though)

    courses.append(course)
    grade.append(grade)
    # Small advantage: If the user exits after entering a course, that course 
    # is not added to the file. In the other case you might have a course without grade.
ikom
  • 166
  • 6