2016-04-10 31 views
-2

Ich habe vor kurzem versucht, einen Tamagotchi-Klon in Python zu machen, aber ich brauche Hilfe, da ich einfach nicht herausfinden kann, wie man den Code nach einem bestimmten Punkt wieder an den Start bringen kann. Ich versuchte, dieProgramm in Python neu starten

while == True: 

Art der Methode, und ich verstehe es ich weiß einfach nicht, wie sie es bekommen mit meinem Code arbeiten. Unten ist mein Code - Es ist ziemlich lang, bitte entschuldigen Sie die Länge. Ich habe es ziemlich ausführlich kommentiert und es sollte einfach zu lesen sein. Jede Hilfe würde sehr geschätzt werden. :)

# 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.") 

Ich brauche den Code Neustart nach allen drei von diesen haben, wenn Aussagen (so nach dem 0 Energie erreicht, Glück, oder wenn das Tier 100 Ausbildung erreicht)

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!") 

Danke sehr viel im Voraus :) Marcell

+0

Bearbeiten Sie den Code nur in den Teilen, die zur Reproduktion des Problems erforderlich sind (z. B. könnte Ihr Problem möglicherweise mit weniger als fünf verschiedenen Tierarten reproduziert werden). – TigerhawkT3

+0

Ich muss TigerhawkT3 zustimmen. Es ist schade, weil ich deine Haustiere mag;) BTW, 'while == True:' ist keine gültige Syntax; es sollte "während True" sein: ' – zondo

+0

Sorry darüber. Ich bin froh, dass dir die Tiere gefallen haben: p – Excelize

Antwort

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.") 
0

Sie die richtige Idee haben, haben Sie in der Tat ein while True: benötigen. Legen Sie die Zeile an die Stelle, an die das Programm zurückkehren soll, nachdem das Haustier gestorben ist. Sie können dann eine Break-Anweisung verwenden, wie Sie bereits aus der ersten Schleife ausbrechen und an die Spitze zurückkehren, um ein neues Haustier zu erstellen.