Python Forum
Repeating lines of code by an input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repeating lines of code by an input
#1
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")
Reply
#2
you have lost all indentation.
Please repost code within code tags 'insert code' on toolbar.
Reply
#3
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")
        
Reply
#4
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 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 644 Dec-12-2023, 09:09 AM
Last Post: buran
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 972 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  repeating a user_input astral_travel 17 2,138 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  Code won't break While loop or go back to the input? MrKnd94 2 906 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Regular Expression search to comment lines of code Gman2233 5 1,593 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Help Switching between keyboard/Mic input in my code Extra 1 1,045 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  if else repeating Frankduc 12 2,425 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,198 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,046 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
  python seems to be skipping lines of code alansandbucket 1 4,088 Jun-22-2021, 01:18 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020