2016-08-15 197 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