重新启动Python程序

时间:2014-05-04 09:29:33

标签: python

我知道之前有人问过,但我还没有理解答案。

我的代码是:

# Code by Jake Stringer
# 2014

import os
print("                       Hailstone Numbers ")
print("")
print("An interesting area of Maths - demonstrated by Jake Stringer")
print("")
input("Please press enter to continue.")

os.system('CLS')

print("Hailstone Numbers:")
print("")
print("RULE 1: Start with any number.")
print("If it is even > divide it by 2.")
print("If it is odd > multiply it by 3, then add 1")
print("")
print("RULE 2: Keep going")
print("")
print("RULE 3: You will always end up at 1.")
print("")
print ("Let's put Hailstone Numbers into action.")
print("")

user = int(input("Please enter any number: "))

print("")

os.system('CLS')


print (user)
print("")

if user % 2 == 0 : 

    print("What you entered is an even number.")
    print("")

    print("So, according to RULE 1, we must now divide it by 2.")

    user = int(user)/2
    thing2 = "So, now we're left with " + str(user) + "."

    print (thing2)
else : 

    print("What you entered is an odd number.")
    print("")

    print("So, according to RULE 1, we must now multiply it by 3, and add 1.")

    user = int(user)*3
    user += 1

    thing2 = "So, now we're left with " + str(user) + "."
    print (thing2)

input("Please press enter to continue.")
os.system('CLS')

print("According to RULE 2, we need to keep on going.")

while user > 1:
    thing3 = "We are currently on the number " + str(user) + "."
    print (thing3)

    if user % 2 == 0 : 

        print("This number is an even number.")
        print("")

        print("So, according to RULE 1, we must now divide it by 2.")

        user = int(user)/2
        thing2 = "So, now we're left with " + str(user) + "."

        print (thing2)
    else : 

        print("This number is an odd number.")
        print("")

        print("So, according to RULE 1, we must now multiply it by 3, and add 1.")

        user = int(user)*3
        user += 1

        thing2 = "So, now we're left with " + str(user) + "."
        print (thing2)
    print("Now we will continue.")
    input("Please press the enter key.")
    os.system('CLS')
    print("")

print(user)
print("")
print("As you can see, you have ended up on the number 1.")
print("")
print("                       Hailstone Numbers ")
print("")
print("An interesting area of Maths - demonstrated by Jake Stringer")
print("")
restart = input("Would you like a try the program again??")

if restart == "yes":
    # restart code here!
else:
# quit the program!

....那么,我该如何让程序重启?我在过去的回答中看到了使用一些代码,当我试用时,说" sys"没有定义(所以我猜它不存在)。还应该指出,我是Python的初学者。谢谢你的帮助!!

2 个答案:

答案 0 :(得分:3)

将整个事物粘贴在函数中,然后在函数外部放置一个重复循环。

import os

def my_function():
    print("                       Hailstone Numbers ")
    print("")
    # more code here
    print("An interesting area of Maths - demonstrated by Jake Stringer")
    print("")


while True:
    my_function()
    restart = input("Would you like a try the program again??")
    if restart == "yes":
        continue
    else:
        break

顺便说一句,您可能希望转到Code Review并让一些人查看您的代码。

答案 1 :(得分:0)

您可以将其置于循环中,如下所示:

# Code by Jake Stringer
# 2014

import os
restart = 'yes'
while restart in ('yes', 'Yes', 'y', 'Y'):
    print("                       Hailstone Numbers ")
    print("")
    print("An interesting area of Maths - demonstrated by Jake Stringer")
    print("")
    input("Please press enter to continue.")

    os.system('CLS')

    print("Hailstone Numbers:")
    print("")
    print("RULE 1: Start with any number.")
    print("If it is even > divide it by 2.")
    print("If it is odd > multiply it by 3, then add 1")
    print("")
    print("RULE 2: Keep going")
    print("")
    print("RULE 3: You will always end up at 1.")
    print("")
    print ("Let's put Hailstone Numbers into action.")
    print("")

    user = int(input("Please enter any number: "))

    print("")

    os.system('CLS')


    print (user)
    print("")

    if user % 2 == 0 : 

        print("What you entered is an even number.")
        print("")

        print("So, according to RULE 1, we must now divide it by 2.")

        user = int(user)/2
        thing2 = "So, now we're left with " + str(user) + "."

        print (thing2)
    else : 

        print("What you entered is an odd number.")
        print("")

        print("So, according to RULE 1, we must now multiply it by 3, and add 1.")

        user = int(user)*3
        user += 1

        thing2 = "So, now we're left with " + str(user) + "."
        print (thing2)

    input("Please press enter to continue.")
    os.system('CLS')

    print("According to RULE 2, we need to keep on going.")

    while user > 1:
        thing3 = "We are currently on the number " + str(user) + "."
        print (thing3)

        if user % 2 == 0 : 

            print("This number is an even number.")
            print("")

            print("So, according to RULE 1, we must now divide it by 2.")

            user = int(user)/2
            thing2 = "So, now we're left with " + str(user) + "."

            print (thing2)
        else : 

            print("This number is an odd number.")
            print("")

            print("So, according to RULE 1, we must now multiply it by 3, and add 1.")

            user = int(user)*3
            user += 1

            thing2 = "So, now we're left with " + str(user) + "."
            print (thing2)
        print("Now we will continue.")
        input("Please press the enter key.")
        os.system('CLS')
        print("")

    print(user)
    print("")
    print("As you can see, you have ended up on the number 1.")
    print("")
    print("                       Hailstone Numbers ")
    print("")
    print("An interesting area of Maths - demonstrated by Jake Stringer")
    print("")
    restart = input("Would you like a try the program again??")
相关问题