功能过早显示输出

时间:2017-01-21 13:09:33

标签: python

以下函数获取一个随机数:

def random_ticket():
    ticket = randint(100, 999)
    print(ticket)

以下函数是使用随机数函数的联系表单:

def contact_form():
    print("We weren't able to find a solution to the problem you are having. Please fill out this short contact form and our personal technicion will contact you as soon as possible!\n")
    name = raw_input('Name: ')
    email = raw_input('Email: ')
    problem_description = raw_input('Describe your problem: ')
    print("\nThank you for filling out this contact form. You will soon be contacted through Email. Here is your ticket number: {}\n".format(random_ticket()))

但是,控制台会在实际“感谢填写文本”之前显示票号。它应该追随那段文字。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

改变这个:

def random_ticket():
    ticket = randint(100, 999)
    print(ticket)

对此:

def random_ticket():
    ticket = randint(100, 999)
    return ticket

在主叫行中:

print("\n... {}\n".format(random_ticket()))

您调用了random_ticket已打印出ticket并返回None的{​​{1}}函数,因此除了已打印ticket之外,您还打印了None线。

答案 1 :(得分:0)

你应该从random_ticket函数返回值

def random_ticket():
    ticket = random.randint(100, 999)
    return (ticket)

它会按预期工作