2016-08-15 2 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) 

Python-Code wie oben, das ist nur der erste Teil des Programms, das Sie in den freien Raum und ersetzen Sie die ..... mit dem Wort, das Sie eingegeben füllen fragt, ob Ihr Antwort ist richtig. Aber als ich das Programm startete, nachdem ich 1 eingegeben hatte, zeigte die Frage wie erwartet, aber nachdem ich "print" (ohne "") für die Antwort für die erste Variable "" eingegeben hatte, druckte es nicht Zeichenfolge mit ersetzten Wörtern.-Code doesnt Druck etwas

+0

Es scheint etwas zu fehlen, die Variable 'finish1' ist nirgends definiert. – fjarri

Antwort

0

Das einzige Problem, das Sie verwendet haben, wenn Level == "1": Ersetzen Sie es durch If Level == 1: und es hat funktioniert.

""" 
    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

Level 1 nimmt int als Eingabe und Sie haben "1" verwendet, was zu einem Zeichen wird, so dass es die Bedingung erfüllt, wenn die Bedingung nicht ausgeführt wurde. –

1

Wenn Sie über die Hürde zu bekommen Ebene 1 Arbeit zu machen, glaube ich, die Struktur des Programms macht es schwierig, die anderen Ebene zu erhalten, wie auch ohne viele redundanten Code zu arbeiten. Ich habe es überarbeitet ein wenig unter die anderen Ebenen zu ermöglichen - nachsehen, ob das gibt Ihnen Ideen in Bezug darauf, mit Ihrem Programm zu bewegen:

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!") 

Einige spezifische Probleme mit Ihrem Code: replacevar() eine break hat die wird nie nach einem return erreicht werden; replacevar() gibt manchmal einen booleschen Wert und manchmal eine Zeichenkette zurück - sie sollte eine Zeichenkette oder None zurückgeben, oder sie sollte True oder False zurückgeben, aber nicht Typen bei Rückkehr mischen; Ihr Hauptprogramm ignoriert das Ergebnis von replacevar(), das es nicht sollte, da es Sie für das nächste Leerzeichen einrichtet; Sie split() die Zeichenfolge aber nicht die Ergebnisse des Aufrufs speichern, so ist es ein No-Op; Sie rufen join() auf einer Zeichenfolge auf, wenn es in einem Array aufgerufen werden soll.

+0

Vielen Dank für Ihre Antwort, aber ich möchte auch wissen, braucht keiner der Python-Code ein ";" Am Ende? Und ist der Abstand wichtig, bevor die Aussagen eingegeben werden? –

+0

@AlanLin, das Ende des Anweisungssemikolons wird selten in Python verwendet und selten gut verwendet, selbst wenn es ist. Vermeide es. Python ist insofern etwas einzigartig, als der Abstand/Einzug vor jeder Zeile semantisch signifikant ist. Es geht nicht so sehr darum, wie viel Abstand/Eindruck es gibt, es geht um Konsistenz. – cdlane