代码不打印任何东西

时间:2016-08-15 04:31:46

标签: python printing

# -*- 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 .....如果您的答案是正确。但是当我运行程序时,在输入1之后,问题显示为预期,但在我键入" print"(没有"")作为第一个变量的答案& #34; 1 ",它不会打印带有替换字词的字符串。

2 个答案:

答案 0 :(得分: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()有一个breakreturn后永远无法访问; replacevar()有时会返回一个布尔值,有时会返回一个字符串 - 它应该返回一个字符串或None,或者它应该返回TrueFalse,但不要混合返回时的类型;你的主程序忽略replacevar()的结果,它不应该为你设置下一个空白;你split()字符串,但无法保存通话结果,所以它是一个无操作;如果要在数组上调用字符串,请在字符串上调用join()

答案 1 :(得分:0)

唯一的问题是你使用if level ==“1”:,如果level == 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)