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
#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


Messages In This Thread
RE: Repeating lines of code by an input - by deanhystad - Oct-15-2020, 01:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 754 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 1,110 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  repeating a user_input astral_travel 17 2,400 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  Code won't break While loop or go back to the input? MrKnd94 2 1,013 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Regular Expression search to comment lines of code Gman2233 5 1,740 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Help Switching between keyboard/Mic input in my code Extra 1 1,131 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  if else repeating Frankduc 12 2,590 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,192 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
  python seems to be skipping lines of code alansandbucket 1 4,217 Jun-22-2021, 01:18 AM
Last Post: Larz60+
  Running a few lines of code as soon as my timer ends nethatar 3 2,455 Feb-26-2021, 01:02 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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