2016-08-15 1 views
0
# -*- coding: utf-8 -*- 
""" 
fill-in-the-blanks1.py 

""" 

# -*- coding: utf-8 -*- 
""" 
p1 

""" 

level1 = '''The __1__ command can print out any types of a variable. __2__ defines a function that could be 
called out at any place in the document. A __3__ is a group of strings, variables or numbers. __3__ can also be 
printed by the __1__ command. __4__ is the statement of true or false.''' 

level2 = '''A ___1___ is created with the def keyword. You specify the inputs a ___1___ takes by 
adding ___2___ separated by commas between the parentheses. ___1___ by default return ___3___ if you 
don't specify the value to return. ___2___ can be standard data types such as string, number, dictionary, 
tuple, and ___4___ or can be more complicated such as objects and lambda functions.''' 

level3 = '''__1__ , __2__ , __3__ , all belongs to the if statement. __1__ will be used at the beginning of the 
if statement, __2__ will be used in the middle between the __4__ and the __5__ statement. ''' 
variables1 = ["print", "def", "list", "boolean"] 
variables2 = ["function", "parameter", "false", "list"] 
variables3 = ["if", "elif", "else", "first"] 

d = "__1__" 
e = "__2__" 
f = "__3__" 
g = "__4__" 
h = "__5__" 
def replacevar(string, variable, inputa, finish): 
    string.split() 
    while True: 
     if inputa == variable[0]: 
      string = string.replace(d, inputa) 
      finish = "" 
      finish = finish.join(string) 
      return finish 
      break;  

     else: 
      print ("Your Answer is incorrect, pls try again") 
      return True 


level = input("Which level do you want to play? (1, 2 or 3)") 
if level == "1": 
    print (level1) 
    useranswer = input("Please enter the value for variable NO.1: ") 
    replacevar(level1, variables1, useranswer, finish1) 
    print (finish1) 

code Python ci-dessus, c'est juste la première partie du programme qui vous demande de remplir le vide et le remplacer ..... avec le mot que vous avez entré si votre la réponse est correcte. Mais quand j'ai lancé le programme, après avoir tapé 1, la question a montré comme prévu, mais après avoir tapé "print" (sans le "") pour la réponse pour la première variable "", il n'imprime pas le chaîne avec des mots remplacés.code ne marche pas imprimer ce

+0

Il semble qu'il manque quelque chose, la variable 'finish1' n'est définie nulle part. – fjarri

Répondre

0

Le seul problème était que vous utilisiez si level == "1":, remplacez-le par if level == 1: et cela a fonctionné.

""" 
    fill-in-the-blanks1.py 

    """ 

# -*- coding: utf-8 -*- 
""" 
p1 

""" 

level1 = '''The __1__ command can print out any types of a variable. __2__ defines a function that could be 
called out at any place in the document. A __3__ is a group of strings, variables or numbers. __3__ can also be 
printed by the __1__ command. __4__ is the statement of true or false.''' 

level2 = '''A ___1___ is created with the def keyword. You specify the inputs a ___1___ takes by 
adding ___2___ separated by commas between the parentheses. ___1___ by default return ___3___ if you 
don't specify the value to return. ___2___ can be standard data types such as string, number, dictionary, 
tuple, and ___4___ or can be more complicated such as objects and lambda functions.''' 

level3 = '''__1__ , __2__ , __3__ , all belongs to the if statement. __1__ will be used at the beginning of the 
if statement, __2__ will be used in the middle between the __4__ and the __5__ statement. ''' 
variables1 = ["print", "def", "list", "boolean"] 
variables2 = ["function", "parameter", "false", "list"] 
variables3 = ["if", "elif", "else", "first"] 

d = "__1__" 
e = "__2__" 
f = "__3__" 
g = "__4__" 
h = "__5__" 
def replacevar(string, variable, inputa, finish): 
    string.split() 
    while True: 
     if inputa == variable[0]: 
      string = string.replace(d, inputa) 
      finish = "" 
      finish = finish.join(string) 
      return finish 
      break;  

     else: 
      print ("Your Answer is incorrect, pls try again") 
      return True 


level = input("Which level do you want to play? (1, 2 or 3)") 
if level == 1: 
    print (level1) 
    useranswer = input("Please enter the value for variable NO.1: ") 
    replacevar(level1, variables1, useranswer, finish1) 
    print (finish1) 
+0

niveau 1 prend int comme entrée et vous utilisiez "1" qui devient un caractère donc il remplit si condition donc si la condition n'a pas été exécutée. –

1

Lorsque vous obtenez sur l'obstacle de faire du travail de niveau 1, je crois que la structure de votre programme, il est difficile d'obtenir les autres niveaux de travailler et sans beaucoup de code redondant. Je l'ai retravaillé un peu en dessous pour permettre aux autres niveaux - voir si cela vous donne des idées par rapport à aller de l'avant avec votre programme:

statements = { 
    "1": '''The __1__ command can print out any type of a variable. __2__ defines a function that 
could be called out at any place in the document. A __3__ is a group of strings, variables or numbers. 
__3__ can also be printed by the __1__ command. __4__ is a statement of true or false.''', 

    "2": '''A __1__ is created with the def keyword. You specify the inputs a __1__ takes by 
adding __2__ separated by commas between the parentheses. __1__ by default return __3__ if you 
don't specify the value to return. __2__ can be standard data types such as string, number, 
dictionary, tuple, and __4__ or can be more complicated such as objects and lambda functions.''', 

    "3": '''__1__ , __2__ , __3__ , all belong to the if statement. __1__ will be used at the beginning 
of the if statement, __2__ will be used in the middle between the __4__ and the __5__ statement.''', 
} 

answers = { 
    "1": [("__1__", "print"), ("__2__", "def"), ("__3__", "list"), ("__4__", "boolean")], 
    "2": [("__1__", "function"), ("__2__", "parameters"), ("__3__", "false"), ("__4__", "list")], 
    "3": [("__1__", "if"), ("__2__", "elif"), ("__3__", "else"), ("__4__", "first")], 
} 

def replacevar(string, answer, response, blank): 

    if response == answer: 
     return string.replace(blank, response) 

    return None 

level = input("Which level do you want to play? (1, 2 or 3) ") 

statement = statements[level] 

blanks = answers[level] 

print(statement) 

for (blank, answer) in blanks: 
    while True: 
     user_answer = input("Please enter the value for blank " + blank + " ") 

     finish = replacevar(statement, answer, user_answer, blank) 

     if finish is None: 
      print("Your Answer is incorrect, please try again") 
     else: 
      statement = finish 
      print(statement) 
      break 

print("Level completed!") 

Quelques problèmes spécifiques avec votre code: replacevar() a une break qui ne sera jamais atteint après un return; replacevar() renvoie parfois un booléen et parfois une chaîne - il doit renvoyer une chaîne ou None, ou il doit renvoyer True ou False, mais ne pas mélanger les types au retour; votre programme principal ignore le résultat de replacevar(), ce qu'il ne devrait pas faire car il vous prépare pour le prochain espace; vous split() la chaîne, mais ne parviennent pas à enregistrer les résultats de l'appel, c'est donc un no-op; vous appelez join() sur une chaîne lorsqu'il est destiné à être appelé sur un tableau.

+0

Merci beaucoup pour votre réponse, mais je veux aussi savoir, aucun code python n'a besoin d'un ";" à la fin? Et l'espacement avant de taper les déclarations importe-t-il? –

+0

@AlanLin, la fin du point-virgule de l'instruction est rarement utilisée en Python et rarement utilisée même quand elle l'est. L'éviter. Python est quelque peu unique en ce que l'espacement/indentation avant chaque ligne est sémantiquement significatif. Il ne s'agit pas tant d'espacement/d'indentation que de cohérence. – cdlane