如何使Python程序再次执行自身? [英] How do I make the Python program to execute itself again?

查看:1276
本文介绍了如何使Python程序再次执行自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,我正在尝试编写一个程序,该程序一直在运行,要求输入新的内容.我想创建一个文件,以便它打开命令提示符,要求用户输入一些值.用户插入输入,程序返回答案,然后重新启动,因此用户将能够插入新输入以获得新答案.直到用户关闭命令窗口为止.

I'm new to Python and I'm trying to write a program that keep running asking for new inputs. I want to create a file such that it opens the command prompt asking the user to input some values. The user inserts the inputs, the program returns a answer and it restarts so the user will be able to insert new inputs to get new answers. It will be done until the user closes the command window.

我创建了一个代码,该代码为我提供了公历中任何日期的工作日.我使用了约翰·康威(John Conway)的世界末日算法"来编写程序.当我运行它时,它工作正常.我创建了一个输入部分,程序在其中要求输入日,月和年.看一下我的以下代码:

I created a code that give to me the weekday of any date in Gregorian Calendar. I used John Conway's "Doomsday Algorithm" to write the program. It works fine when I run it. I created a input section where the program asks for the Day, Month and Year. Look my following code:

#The first part of my doomsday algorithm here (this is to large to simple paste here).
#The last part is creating the last function, that will evaluate everything

def semana(d,m,a):

#definition of the function "semana". 
#I'm Brazilian and this is the portuguese word for "week". 
#Then I insert the input strings here:

x=eval(input("Dia:"))
y=eval(input("Mês:"))
z=eval(input("Ano:"))

semana(x,y,z)

我在命令提示符下运行程序,并输入变量xyz的值,按Enter键,程序显示正确的答案,但在答案出现后它会自行终止

I run the program on command prompt and I enter the values for the variables x,y and z, I press enter and the program shows the correct answer, but it terminates itself right after the answer appears.

我想知道如何使程序在同一窗口中重新启动.我的意思是:我插入xyz的值.然后按Enter键,程序将显示答案.然后它再次要求输入,这样我就可以继续插入值并接收工作日作为答案.

I want to know how to make the program to restart in the same window. What I mean is: I insert the values for x,y and z. Then I press enter and the program shows the answer. Then it asks again for the input so I will be able to keep inserting values and receiving the weekday as answer.

提前谢谢!

推荐答案

您正在寻找的是 while 循环.只要条件为True,此控制结构就允许我们执行一组语句.如果条件变为False,我们就会跳出循环.

What your looking for is a while loop. This control structures allows us to execute a set of statements as long as a condition is True. If the condition becomes False, we break out of the loop.

# -*- encoding: utf-8 -*-

def semana():
    x=input("Dia:")
    y=input("Mes:")
    z=input("Ano:")
    print('{}/{}/{}'.format(x,y,z))

while True:
    semana()

示例输出

Dia:6
Mes:14
Ano:2019
6/14/2019

这篇关于如何使Python程序再次执行自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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