Python Forum

Full Version: Repeating lines of code by an input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I'm currently learning python in school so am relatively new. I'm working on my own code that generates a random character with its own stats that you can upgrade. I want the player to be able to input something that will repeat lines 39 - 86 is there a way to do this?
import random
import time
stat1 = random.randint(4, 30)
stat2 = random.randint(4, 30)
stat3 = random.randint(4, 30)

ttl = stat1 + stat2 + stat3

while ttl != 30:
stat1 = random.randint(4, 30)
stat2 = random.randint(4, 30)
stat3 = random.randint(4, 30)
ttl = stat1 + stat2 + stat3

if(ttl == 30):
print("Strength: " + str(stat1))
print("Agility: " + str(stat2))
print("Heatlh: " + str(stat3))
print("Total Stats: " + str(ttl) + "\n")

import random

time.sleep(2)
hat = random.choice([" ^", " *", " L", " M", " H"])
head = random.choice([" []", " ()", " ||", " {}"])
Body = random.choice(["{}", "|)", "[]", "()"])
arm1 = random.choice(["/", "|", "<", "("])
arm2 = random.choice(["\\", "|", ">", ")"])
Torso = random.choice([" {}", " |)", " []", " ()"])
print(hat)
print(head)
print(arm1, Body, arm2)
print(Torso)
print(" ||")
print(" LL")

time.sleep(2)

ansr = input("Improve Stats? (Y/N) ")
if(ansr == "Y" or ansr == "y"):
time.sleep(1)
stchoice = input("Improve which Stat? (Strength, Agility, Health):\n ")
if(stchoice == "Strength" or stchoice == "strength"):
time.sleep(1)
print("Play this mini game to improve your Strength!\n")
time.sleep(2)
print("Convert this number to a word")
print("When entering word don't use capitalization")
time.sleep(1)
word = (random.choice(["100000.000000", "10000000000.", "10000000000000.0"]))
time.sleep(1)
print(word)
if(word == "100000.000000"):
word = ("one hundred thousand")
ansrnum = input("Enter the converted number: ")
if(ansrnum == "one hundred thousand"):
print("Correct!")
newst1 = (5 + stat1)
print("New strength stat is: " + str(newst1))
time.sleep(1)
print("Total Stat is: " + str(ttl + 5))
else:
print("Incorrect")
if(word == "10000000000."):
word = ("ten billion")
ansrnum = input("Enter the converted number: ")
if(ansrnum == "ten billion"):
print("Correct!")
newst1 = (5 + stat1)
print("New strength stat is: " + str(newst1))
time.sleep(1)
print("Total Stat is: " + str(ttl + 5))
else:
print("Incorrect")
if(word == "10000000000000.0"):
word = ("ten trillion")
ansrnum = input("Enter the converted number: ")
if(ansrnum == "ten trillion"):
print("Correct!")
newst1 = (5 + stat1)
time.sleep(2)
print("New strength stat is: " + str(newst1))
time.sleep(1)
print("Total Stat is: " + str(ttl + 5))
else:
print("Incorrect")
you have lost all indentation.
Please repost code within code tags 'insert code' on toolbar.
Hi I'm currently learning python in school so am relatively new. I'm working on my own code that generates a random character with its own stats that you can upgrade. I want the player to be able to input something that will repeat lines 39 - 86 is there a way to do this?
import random
import time
stat1 = random.randint(4, 30)
stat2 = random.randint(4, 30)
stat3 = random.randint(4, 30)

ttl = stat1 + stat2 + stat3

while ttl != 30:
    stat1 = random.randint(4, 30)
    stat2 = random.randint(4, 30)
    stat3 = random.randint(4, 30)
    ttl = stat1 + stat2 + stat3

if(ttl == 30):
    print("Strength: " + str(stat1))
    print("Agility: " + str(stat2))
    print("Heatlh: " + str(stat3))
    print("Total Stats: " + str(ttl) + "\n")

import random 

time.sleep(2)
hat = random.choice(["  ^", "  *", "  L", "  M", "  H"])
head = random.choice(["  []", "  ()", "  ||", "  {}"])
Body = random.choice(["{}", "|)", "[]", "()"])
arm1 = random.choice(["/", "|", "<", "("])
arm2 = random.choice(["\\", "|", ">", ")"])
Torso = random.choice(["  {}", "  |)", "  []", "  ()"])
print(hat)
print(head)
print(arm1, Body, arm2)
print(Torso)
print("  ||")
print("  LL")

time.sleep(2)

ansr = input("Improve Stats? (Y/N) ")
if(ansr == "Y" or ansr == "y"):
    time.sleep(1)
    stchoice = input("Improve which Stat? (Strength, Agility, Health):\n ")
    if(stchoice == "Strength" or stchoice == "strength"):
        time.sleep(1)
        print("Play this mini game to improve your Strength!\n")
        time.sleep(2)
        print("Convert this number to a word")
        print("When entering word don't use capitalization")
        time.sleep(1)
        word = (random.choice(["100000.000000", "10000000000.", "10000000000000.0"]))
        time.sleep(1)
        print(word)
        if(word == "100000.000000"):
            word = ("one hundred thousand")
            ansrnum = input("Enter the converted number: ")
            if(ansrnum == "one hundred thousand"):
                print("Correct!")
                newst1 = (5 + stat1)
                print("New strength stat is: " + str(newst1))
                time.sleep(1)
                print("Total Stat is: " + str(ttl + 5))
            else:
                print("Incorrect")
        if(word == "10000000000."):
            word = ("ten billion")
            ansrnum = input("Enter the converted number: ")
            if(ansrnum == "ten billion"):
                print("Correct!")
                newst1 = (5 + stat1)
                print("New strength stat is: " + str(newst1))
                time.sleep(1)
                print("Total Stat is: " + str(ttl + 5))
            else:
                print("Incorrect")
        if(word == "10000000000000.0"):
            word = ("ten trillion")
            ansrnum = input("Enter the converted number: ")
            if(ansrnum == "ten trillion"):
                print("Correct!")
                newst1 = (5 + stat1)
                time.sleep(2)
                print("New strength stat is: " + str(newst1))
                time.sleep(1)
                print("Total Stat is: " + str(ttl + 5))
            else:
                print("Incorrect")
        
You need to learn about functions.  Functions break up large blocks of code into smaller, easier to understand chunks.  These chunks can be reused as much as you need by calling the function.  In the example below, the improve_stats() function calls the mini_game() function.  This separates the mini-game from the improve stats logic.  You could design a more interesting or entertaining game without having to change an of the other code.
 
def mini_game():
    ## blah blan blah
    if correct_answer:
        return 5
    return 0

def improve_stats():
    stchoice = input("Improve which Stat? (Strength, Agility, Health):\n ")
    score = mini_game()
    if score != 0:
        if stchoice[0] in 'sS':
            strength = min(MAX_STRENGTH, strength+score)
            print("Your new strength stat is", strength)
        elif strchoice[0] in 'aA':
            agility = min(MAX_AGILITY, agility+score)
            print("Your new agility stat is", agility)
        ## blah blah blah