Python Forum
im not sure what ive done wrong code doesnt run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
im not sure what ive done wrong code doesnt run
#1
def main():
    again='y'
    while again=='y' or again=='Y':
        try:
            key=('A', 'C', 'A', 'B', 'B', 'D', 'D', 'A', 'C', 'A',
'B', 'C', 'D', 'C', 'B')
            while again=='y':
                print('_-_-_-_-_-'*5)
                name=input('Enter student name:')

                filename=input('\tEnter student file name: ')
                file=open(filename, 'r')
                i=0
                choice=file.readline()
                while choice!='':
                    choice=choice.rstrip('\n')
                    ans[i]=choice
                    choice=file.readline()
                    i+=1
                    correct=0
main()
Reply
#2
It doesn't run because you have a try without a matching except

There are a few issues with your code for starters:
again is assigned as 'y' and never re-assigned so the first while loop will always be True.
The second while loop is not required.
key is repeatedly assigned on every while loop iteration and is never used.
A file is opened and never closed
ans is not defined
Reply
#3
[removed] posted at the exact same time as Yoriz
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
You don't need three while loops; one loop for entering the student name and file, one loop for reading the file. Actually, I would make the inner loop a for loop.
for line in file:
It is much easier using the file iterator than using readline().

You should close you file when you are done with it. Either use file.close() or use a context manager (with open(filename, "r") as file:).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what is wrong with my code 53535 4 1,558 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 2,234 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 2,680 Dec-09-2020, 06:37 AM
Last Post: stranac
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,707 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Something is Wrong with my code susmith552 4 3,047 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 2,388 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  It worked now it doesnt...ugh raymond2688 15 5,491 Aug-07-2019, 07:47 PM
Last Post: raymond2688
  A program for backing up documents (doesnt work(code is inside)) Richard_SS 4 3,429 Jun-05-2019, 03:47 PM
Last Post: heiner55
  Wrong output on my code. JTNA 2 7,920 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Why is this code wrong? Lemmy 4 5,185 Apr-05-2018, 03:46 PM
Last Post: Lemmy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020