Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中创建循环?_Python_Python 3.x - Fatal编程技术网

如何在Python中创建循环?

如何在Python中创建循环?,python,python-3.x,Python,Python 3.x,我刚刚用python3在PyScripter中启动了一个非常基本的程序。我要求用户输入一个数字,我的程序计算这个数字。这一切都运行得很好,但我想知道,如果有人希望程序再次运行或evit,他们是否知道如何使用python作为用户。如果是这样,我如何再次运行计算器,以及如何根据请求退出计算器。例如“你想再次运行程序吗?键入是或否*如果是,我如何再次执行相同的代码如果否,如何停止执行。谢谢你的帮助 Number = int(input("Number Required")) if Number &

我刚刚用python3在PyScripter中启动了一个非常基本的程序。我要求用户输入一个数字,我的程序计算这个数字。这一切都运行得很好,但我想知道,如果有人希望程序再次运行或evit,他们是否知道如何使用python作为用户。如果是这样,我如何再次运行计算器,以及如何根据请求退出计算器。例如“你想再次运行程序吗?键入是或否*如果是,我如何再次执行相同的代码如果否,如何停止执行。谢谢你的帮助

Number = int(input("Number Required"))

 if Number >= 1 and Number<=50:
  add =(Number * 5)
  print("The Number amount it", add)

 elif Number <= 80 and Number >= 50:
   add =(Number * 4)
   print("The Number amount it", add)

 elif Number <= 100 and Number >= 80:
   add =(Number * 2.5)
   print("The Number amount it", add)

 run = input("would you like to check again type yes or no")
Number=int(输入(“需要数字”))

如果数字>=1且数字一旦流程完成,它就无法自行启动-它需要另一个流程或服务来再次启动,但从您如何提出问题的角度来看,您只需要在不退出流程的情况下循环某些逻辑

更新问题时更新我的答案

while True:
    x = input("Enter your input: \n")
    some_logic_here()

将所有代码添加到
while
循环中

repeat = True
while repeat:
    Number = int(input("Number Required"))

    if Number >= 1 and Number<=50:
      add =(Number * 5)
      print("The Number amount it", add)

    elif Number <= 80 and Number >= 50:
       add =(Number * 4)
       print("The Number amount it", add)

    elif Number <= 100 and Number >= 80:
       add =(Number * 2.5)
       print("The Number amount it", add)

    run = input("would you like to check again type yes or no")
    if run == 'no': repeat = False
repeat=True
重复时:
数字=整数(输入(“所需数字”))

如果Number>=1,Number=1,Number我编写了一个简单的计算器程序,它具有我认为您在这里寻找的那种功能:

为了保持简单,(许多选项中的)一个是创建一个基本的Restart()函数

在我的示例中,我在计算后调用Restart()函数:

def Restart():
    toMenu = raw_input("\nTo go back to main menu, type '1'. To exit, type '2'.\n\n")
    if toMenu == '1':
        menu() # My menu function
    elif toMenu == '2':
        exit() # Built-in Python Exit-program option
    else:
        invalid() # My "Invalid input" error message

有一个涵盖所有可能性的单一提示

while True:
    answer = input("Enter number or 'q' to exit")
    if answer == 'q':
        break
    try:
        Number = int(answer)
    except ValueError:
        print("'{}' is not a number".format(answer))
        continue
    ... your calculator here ...

在问题结束前添加代码……快点,如果答案解决了你的问题,也要学会接受答案。看看你在提问之前对此做了多少研究?这个网站上有很多。仅供参考:这将因Python3上的名称错误而失败,这是OP正在使用的。我错过了他在Python3上的消息。如果1