为什么这段代码不返回这些列表?这与使用“返回"而不是“打印"有关吗? [英] Why isn't this code returning these lists? Does this have to do with using 'return' rather than 'print'?

查看:72
本文介绍了为什么这段代码不返回这些列表?这与使用“返回"而不是“打印"有关吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,当我将所有内容都设置为打印"时,该代码即可工作,但是现在通过返回",程序在我选择了主题,主题和问题/答案后终止.我编写了此程序,以在闪存卡中输入问题和答案,然后能够选择主题,然后选择主题,然后选择是仅查看问题,答案,还是同时查看两者.我注意到在每个列表的末尾都会出现无"一词,我试图通过将打印"替换为返回"来解决此问题,但这带来了更多的问题,我非常感谢我提供的一些建议应该做.

So this is my code, when I had everything set to 'print' things, the code worked, but now with 'return' the program terminates after I select a Subject, Topic, and the questions/answers. I made this program to enter in my flash card questions and answers, and then be able to select the subject, then select a topic, and then select whether to see only questions, answers, or see both. I noticed at the end of each list the word 'None' would appear, I tried to fix this by replacing 'print', with 'return', but that has brought up even more problems, I would really appreciate some input on what I should do.

代码在下面

-13岁的伊桑(Ethan)

--Ethan, 13

import sys
subjects = ["History", "Science"]
topics_science = ["Light", "X-mas Exam Review"]
topics_history = ["Italian Renaissance"]
science_xmasreview = ["Q. What do we use to catogorize a habitat?", \
"A. Damp or Dry, Hot or Cold, Windy or Calm, Dim or Bright.", \
"Q. What is something that only eats plants?", \
"A. Herbivore", \
"Q. What are the properties of oxygen?"]

science_light = [
"Q. What is an object the gives out light?", \
"A. Light source", \
"Q. What is the speed of light?", \
"A. 300 million meters per second.", \
"Q. What does transparent mean?", \
"A. Light can pass completely through a transparent material."]

history_renaissance = [
"Q. What did Lorenzo do differently from Cosimo?", \
"A. Lorenzo made no effort to conceal his power", \
"Q. Why did the Pope want Lorenzo dead?", \
"A. Because the Pope wanted more power and saw Lorenzo as a threat.", \
"Q. Who did the Pazzi plot with to kill Lorenzo?", \
"A. Pope Sixtus IV"]

def qanda(x):
    print
    print "Enter 1 for just questions"
    print "Enter 2 for just answers"
    print "Enter 3 for both"
    qa = raw_input("Enter number: ")
    qa = str(qa)
    if qa == '1':
        printer(x[::2])
    elif qa == '2':
       printer(x[1::2])
    elif qa == '3':
        printer(x)
    else:
        print "Not recognized"
def newline():
    raw_input()
    print
def printer(list):
    n = 0
    l = len(list)
    print
    while n < l:
        return list[n]
        newline()
        n += 1
    while n == l:
        n += 1
def subjectchoice():
    if subject == "1":
        print
        history()
    elif subject == "2":
        print
        science()
        else:
        print 'Not recognized.'
def science():
    print topics_science
    print "Enter 1 for Light"
    print "Enter 2 for X-mas Exam Review"
    topicchoice = raw_input("Enter number: ")
    topicchoice = str(topicchoice)
    if topicchoice == "1":
        qanda(science_light)
    elif topicchoice == "2":
          qanda(science_xmasreview)
    else:
        print "Not recognized"
        sys.exit
def history():
    print topics_history
    print "Enter 1 for Italian Renaissance"
    topicchoice = raw_input("Enter number: ")
    topicchoice = str(topicchoice)
    if topicchoice == "1":
       return qanda(history_renaissance)
    else:
        print "Not recognized"
        sys.exit()
print subjects
print "Enter 1 for History"
print "Enter 2 for Science"
subject = raw_input("Enter number: ")
subject = str(subject)
subjectchoice()

推荐答案

我相信这是可行的,您不需要在printer()函数中使用return语句:

I believe this will work, you didn't need the return statement in your printer() function:

import sys
subjects = ["History", "Science"]
topics_science = ["Light", "X-mas Exam Review"]
topics_history = ["Italian Renaissance"]
science_xmasreview = ["Q. What do we use to catogorize a habitat?",
                      "A. Damp or Dry, Hot or Cold, Windy or Calm, Dim or Bright.",
                      "Q. What is something that only eats plants?",
                      "A. Herbivore",
                      "Q. What are the properties of oxygen?"]

science_light = [
    "Q. What is an object the gives out light?",
    "A. Light source",
    "Q. What is the speed of light?",
    "A. 300 million meters per second.",
    "Q. What does transparent mean?",
    "A. Light can pass completely through a transparent material."]

history_renaissance = [
    "Q. What did Lorenzo do differently from Cosimo?",
    "A. Lorenzo made no effort to conceal his power",
    "Q. Why did the Pope want Lorenzo dead?",
    "A. Because the Pope wanted more power and saw Lorenzo as a threat.",
    "Q. Who did the Pazzi plot with to kill Lorenzo?",
    "A. Pope Sixtus IV"]

def printer(list):
    n = 0
    l = len(list)
    print
    while n < l:
        print list[n]  # <-- Changed this from a return to a print statement
        newline()
        n += 1
    while n == l:
        n += 1

def qanda(x):
    print
    print "Enter 1 for just questions"
    print "Enter 2 for just answers"
    print "Enter 3 for both"
    qa = raw_input("Enter number: ")
    qa = str(qa)
    if qa == '1':
        printer(x[::2])
    elif qa == '2':
        printer(x[1::2])
    elif qa == '3':
        printer(x)
    else:
        print "Not recognized"

def newline():
    raw_input()
    print



def subjectchoice():
    if subject == "1":
        print
        history()
    elif subject == "2":
        print
        science()
    else:
        print 'Not recognized.'
def science():
    print topics_science
    print "Enter 1 for Light"
    print "Enter 2 for X-mas Exam Review"
    topicchoice = raw_input("Enter number: ")
    topicchoice = str(topicchoice)
    if topicchoice == "1":
        qanda(science_light)
    elif topicchoice == "2":
        qanda(science_xmasreview)
    else:
        print "Not recognized"
        sys.exit
def history():
    print topics_history
    print "Enter 1 for Italian Renaissance"
    topicchoice = raw_input("Enter number: ")
    topicchoice = str(topicchoice)
    if topicchoice == "1":
        return qanda(history_renaissance)
    else:
        print "Not recognized"
        sys.exit()
print subjects
print "Enter 1 for History"
print "Enter 2 for Science"
subject = raw_input("Enter number: ")
subject = str(subject)
subjectchoice()

这篇关于为什么这段代码不返回这些列表?这与使用“返回"而不是“打印"有关吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆