2016-08-15 153 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代码如上,这仅仅是要求您填写的空白,替代节目的第一部分.....如果与你输入的字你答案是正确的。但是当我运行该程序时,输入1后,问题如预期般显示,但是在输入第一个变量“”的“print”(不带“”)后,它不打印带有替换词的字符串。代码犯规打印任何东西

+0

似乎缺少某些东西,变量'finish1'没有在任何地方定义。 – fjarri

回答

0

唯一的问题是你使用的是如果级==“1”:,取代它如果级== 1:它的工作。

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

级别1将int作为输入,并且您正在使用变成字符的“1”,因此如果条件未被执行,条件满足。 –

1

当你为了使一级工作的障碍,我相信你的程序的结构,使得它很难得到其他层面没有大量的冗余代码正常工作。我在下面重新整理了一下,以使其他级别 - 看看这个让你对于任何的想法与你的计划向前推进:

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

与您的代码的一些具体问题:replacevar()break其将永远不会达到return; replacevar()有时会返回一个布尔值,有时会返回一个字符串 - 它应该返回一个字符串或None,或者它应该返回TrueFalse,但不要混合返回的类型;你的主程序忽略了replacevar()的结果,它不应该为你设置下一个空白;你split()字符串,但未能保存调用的结果,所以这是一个无操作;你打电话给join()的字符串,当它打算在数组上调用时。

+0

非常感谢您的回答,但我也想知道,没有任何Python代码需要“;”最后?在输入语句之前,间距是否重要? –

+0

@AlanLin,语句分号的结尾在Python中很少使用,甚至在很少使用时也很少使用。躲开它。 Python有点独特之处在于每行之前的间距/缩进在语义上是重要的。它与关于一致性的间距/缩进量没有多大关系。 – cdlane