在python中重启程序

时间:2016-04-10 02:34:14

标签: python

我最近一直试图在python中创建一个Tamagotchi克隆,但我需要帮助,因为我无法弄清楚如何让代码在某个点之后回到起点。我试过了

while == True:

一种方法,我理解它我只是不知道如何使用我的代码。下面是我的代码 - 很长,请原谅长度。我评论它非常彻底,应该很容易阅读。任何帮助将不胜感激。 :)

# Modules
import os  # Used for cls function - clean screen
import time  # Used for delaying messages etc.
import sys

# Pet details
running = True

petType = ""
petName = ""
activity = ""

# Pet well being
pet = [60, 60, 0]  # 0 = energy 1 = happiness 2 = training
# Loop counter
i = 0



#
# Animal Art for the activity loop and other parts
#
def cat():  # Stores cat ASCII art - credit to 'jgs' - https://user.xmission.com/~emailbox/ascii_cats.htm
    print('''|
| \    /_
|  )  ( ')
| (  /  )
|  \(__)|
|''')


def bird():  # Cat ASCII - credit to 'as' - http://chris.com/ascii/index.php?art=animals/birds
    print('''|
|   >\_/<
|  _\*v*/_
|  \\   //
| ===="====
|    /^\
|
|''')


def bunny():  # Bunny ASCII - credit to 'Felix Lee' - http://www.chris.com/ascii/index.php?art=animals/rabbits
    print('''|
|  /\ /|
|  \ V /
|  | "")
|  / ` |
|  /   \)
| (__\_\)
|''')


def dog():  # Dog ASCII - credit 'jgs' - http://www.chris.com/ascii/index.php?art=animals/dogs
    print('''|
|          .-._
|         {_}^ )o
|  {\_______//~`
|  (         )
|  /||~~~~~||\,
| |_\.\_    \.\_\_
|''')


def mouse():  # Mouse ASCII - credit 'jgs' - http://www.chris.com/ascii/index.php?art=animals/rodents/mice
    print('''|
|          (\-.
|          / _`>
|  _)     / _)=
| (      / _/
|  `-.__(___)_
|''')


###################

# 'Sleep' are made to be easier to write than 'time.sleep(1)
def sleep():
    time.sleep(1)


# Clears the console
def cls():
    os.system('cls')


# Shows the pet's statistics from user's previous inputs like name, type, energy etc.
def printStats():
    sleep()
    print("|-----------------------------")
    print("| " + petName + "'s Stats: ")
    if petType == "Cat":
        cat()
    elif petType == "Bird":
        bird()
    elif petType == "Bunny":
        bunny()
    elif petType == "Dog":
        dog()
    elif petType == "Mouse":
        mouse()
    print("| Breed: " + petType)

    print("| Energy: " + str(pet[0]))
    print("| Happiness: " + str(pet[1]))
    print("| Training: " + str(pet[2]))
    print("|-----------------------------")


def limiter():
    if pet[0] >= 100:
        pet[0] = 100
    elif pet[1] >= 100:
        pet[1] = 100
    elif pet[2] >= 100:
        pet[2] = 100
#
#
# Introduction Message and start of the actual game
#
#

print("""|-------------------
| Welcome to Tamagotchi!
|-------------------""")
sleep()
print("""Please choose a type of pet from the list below: (enter a,b,c,d or e)
a. Bird
b. Bunny
c. Cat
d. Dog
e. Mouse
""")

# Getting pet's type and processing it
while i == 0:
    sleep()
    petType = input("Which one would you like?: ")
    if petType == "a":
        petType = "Bird"
        sleep()
        print("You chose a " + petType)
        sleep()
        print("Congratulations! Your new pet is ready.")
        bird()
        sleep()
        break  # stops loop
    elif petType == "b":
        petType = "Bunny"
        sleep()
        print("You chose a " + petType)
        sleep()
        print("Congratulations! Your new pet is ready.")
        bunny()
        sleep()
        break  # stops loop
    elif petType == "c":
        petType = "Cat"
        sleep()
        print("You chose a " + petType)
        sleep()
        print("Congratulations! Your new pet is ready.")
        cat()
        sleep()
        break  # stops loop
    elif petType == "d":
        petType = "Dog"
        sleep()
        print("You chose a " + petType)
        sleep()
        print("Congratulations! Your new pet is ready.")
        dog()
        sleep()
        break  # stops loop
    elif petType == "e":
        petType = "Mouse"
        sleep()
        print("You chose a " + petType)
        sleep()
        print("Congratulations! Your new pet is ready.")
        mouse()
        sleep()
        break  # stops loop
    else:
        print("ERROR: That's not a valid pet type.")  # Error message
        sleep()
        petType = input("Which one would you like?: ")
        continue  # Continues the loop

# Name loop
while i == 0:
    petName = input("Please choose a name for your new pet: ")
    if petName.isnumeric():
        print("ERROR: That is not a valid name. Please use only letters.")
        petName = input("Please choose a name for your new pet: ")
        continue  # continues loop
    elif len(petName) >= 25:  # Checks if the entry for name is too long
        print("ERROR: Please do not enter more than 25 characters.")
        petName = input("Please choose a name for your new pet: ")
        continue  # continues loop
    elif petName == "":
        print("ERROR: Please enter a name")
        petName = input("Please choose a name for your new pet: ")
        continue  # continues loop
    else:
        print(petName + "! That's a nice name")
        break  # stops loop

cls()
# Activity loop
while i == 0:
    activity = input("Do you want to (f)eed, (p)lay, (s)leep or (t)rain?: ")
    if activity.isdigit():
        print("ERROR: That is not a valid activity.")
        activity = input("Do you want to (f)eed, (p)lay, (s)leep or (t)rain?: ")
    elif activity == "f":
        pet[0] += 5
        print("")
        print("You fed " + petName + ". +5 Energy.")
        limiter()
        printStats()
        time.sleep(2)
        cls()
    elif activity == "p":
        pet[1] += 10
        pet[0] -= 10
        print("")
        print("You played with " + petName + ". -10 Energy, +10 Happiness.")
        limiter()
        printStats()
        time.sleep(2)
        cls()
    elif activity == "s":
        pet[0] += 20
        pet[1] -= 5
        print("")
        print("You decided to let " + petName + " sleep. +20 Energy, -5 Happiness.")
        limiter()
        printStats()
        time.sleep(2)
        cls()
    elif activity == "t":
        pet[2] += 10
        pet[0] -= 10
        pet[1] -= 5
        print("")
        print("You decided to train " + petName + ". -10 Energy, -5 Happiness, +10 Training.")
        limiter()
        printStats()
        time.sleep(2)
        cls()
    elif pet[0] <= 0:
        print(petName + " died because he reached 0 energy :(")
        break
    elif pet[1] <= 0:
        print(petName + " ran away because he was unhappy :(")
        break
    elif pet[2] == 100:
        print("Congratulations! " + petName + " has reached 100 training!")

    else:
        print("ERROR: That is not a valid activity.")

我需要在所有这三个if语句之后重启代码(所以在它达到0能量,快乐或宠物达到100训练之后)

    elif pet[0] <= 0:
    print(petName + " died because he reached 0 energy :(")
    break
elif pet[1] <= 0:
    print(petName + " ran away because he was unhappy :(")
    break
elif pet[2] == 100:
    print("Congratulations! " + petName + " has reached 100 training!")

非常感谢你提前:) 马塞尔

2 个答案:

答案 0 :(得分:1)

while True:

#
#
# Introduction Message and start of the actual game
#
#

  print("""|-------------------
  | Welcome to Tamagotchi!
  |-------------------""")

#[...]
  # Activity loop
  while True:
  #[...]

    elif pet[0] <= 0:
        print(petName + " died because he reached 0 energy :(")
        break
    elif pet[1] <= 0:
        print(petName + " ran away because he was unhappy :(")
        break
    elif pet[2] == 100:
        print("Congratulations! " + petName + " has reached 100 training!")

    else:
        print("ERROR: That is not a valid activity.")

答案 1 :(得分:0)

你有正确的想法,你确实需要一个while True:。将该行放在您希望程序返回宠物死后的位置。然后你就可以使用像你已经打破第一个循环的break语句,然后回到顶部创建一个新的宠物。