Pardonnez-moi si cela sort un peu de cerveau, je n'exagère pas quand je dis que je travaille sur ce programme depuis plus de 13 heures maintenant et je suis sérieusement privé de sommeil. C'est ma 4ème révision et honnêtement je ne sais plus quoi faire, donc si quelqu'un peut m'aider, ce serait grandement apprécié. Mon introduction à l'enseignant de programmation voulait que nous fassions un programme d'étude «carte flash» à partir de son modèle. J'utilise Idle 3.3.3 sur une machine Windows 7.Le programme Python ne se ferme pas; Importer aléatoire causant des erreurs
#Flash Cards
#Uses parallel arrays to store flash card data read from file
#Quizzes user by displaying fact and asking them to give answer
import random
def main():
answer = [] #array to store answer for each card
fact = [] #array to store fact/definition for each card
totalTried = 0 #stores number of cards attempted
totalRight = 0 #stores number of correct guesses
loadCards(answer, fact) #call loadcards() and pass it both arrays
numCards = len(answer) #find number of cards loaded
keepGoing = "y"
while keepGoing == "y" or keepGoing == "Y":
#Enter your code below this line
# 2a. Pick random integer between 0 and numCards and store the
# number in a variable named randomPick.
randomPick = random.randint (0, numCards)
# 2b. Add one to the totalTried accumulator variable.
totalTried = totalTried + 1
# 2c. Print element randomPick of the fact array. This shows the
# user the fact/definition for this flashcard.
print (fact [randomPick])
# 2d. Prompt the user to input their guess and store the string they
# enter in a variable named "userAnswer"
userAnswer = input ('What is your answer?')
# 2e. Compare the user's guess -userAnswer- to element
# -randomPick- of the answer array.
if userAnswer == (answer [randomPick]):
# 2e-1 If the two strings are equal, tell the user they
# guessed correctly and add 1 to the totalRight
# accumulator variable.
print ('That is correct.')
totalRight == totalRight + 1
# 2e2. If the two strings are not equal, tell the user they guessed
# wrong and display the correct answer from the answer array.
else:
print ('That is incorrect.')
print (answer [randomPick])
#2f. Prompt the user the user to see if they want to continue and
#store their response in the keepGoing variable.
keepGoing = input ('Would you like to continue?')
#Enter your code above this line
print("You got", totalRight, "right out of", totalTried, "attempted.")
def loadCards(answer, fact):
#Enter your code below this line
# 1a. Open flashcards.txt in read mode & assign it var name "infile"
infile = open('flashcards.txt', 'r')
# 1b. Read 1st line from file and store in var. name "line1"
line1 = infile.readline()
# 1c. Use while loop to make sure EoF has not been reached.
while line1 != '':
# 1c1. Strip newline escape sequence (\n)from variable's value.
line1 = line1.rstrip ('\n')
# 1c2. Append string to answer array.
answer.append (line1)
# 1c3. Read next line from file and store in var. name "line2"
line2 = infile.readline()
# 1c4. Strip newline escape sequence (\n) from variable's value.
line2 = line2.rstrip ('\n')
# 1c5. Append the string to the fact array.
fact.append (line2)
# 1c6. Read next line from file and store it in var. name "line3".
line3 = infile.readline()
# 1d. Close file.
infile.close()
#Enter your code above this line
main()
Quand je lance le programme, rien ne se passe réellement, mais quand je tente de fermer la fenêtre de shell après, il me dit que le programme est encore course et demande si je veux le tuer.
Le débogueur ne me montre aucune information lorsque j'essaie de le vérifier, aussi.
Cependant, si je copie le code dans le shell et l'exécute à partir de là, j'obtiens "SyntaxError: plusieurs instructions trouvées lors de la compilation d'une seule instruction". Aucun fichier n'a changé, mais plus tôt, il me disait qu'il y avait un problème avec "import random".
Merci d'avance pour toute aide.
Il est également utile d'inclure la trace réelle, pas seulement l'exception ou, pire, "il y avait un problème". Tracebacks ont des informations vitales, y compris en indiquant la ligne et la position de l'erreur, ce qui rend beaucoup plus facile de vous aider. –
Veuillez reformuler votre question, fournir une trace d'erreur, le code pertinent, l'entrée/sortie attendue. Vous devez nous aider à vous aider. – sshashank124
@Wooble: point valide. Comme je l'ai dit, je n'ai pas beaucoup dormi. Je suis retourné pour éditer mon post mais quelqu'un d'autre avait déjà. – user3598983