Reuse the same function until dice roll is higher

Question:

Can you please help me with my code? I am very fresh in python and coding in general.
My code works well when the number randomly generated is greater then "1".
How do I re-run the functions until the number generated is greater then "1"?

My code is:

from random import randint


def dice_roll_input():
    '''
    Askes the user to type "roll" to run the function that
    simulates rolling the dice.
    '''
    print('Roll more then "1" to kill the python.n')
    dice_conf = input('Type "roll" to roll the dicen')

    if dice_conf == 'roll':
        return
    if dice_conf != 'roll':
        dice_roll_input()


def roll_dice():
    '''
    Simulates rolling the dice and returning the result.
    '''
    random_dice = randint(1,6)
    return random_dice


def kill_python(dice):
    '''
    Based on the dice roll, either python gets killed or
    it dodges the attack and user needs to roll again.
    '''
    if dice == 1:
        print(f'You rolled: {dice} n')
        print('Python dodged your attack. Try again.n')
    else:
        print(f'You rolled: {dice} n')
        print('You killed the python!')


def attack_python():
    '''
    Function to loop through dice rolls until dice>1.
    '''
    dice = 1
    while dice == 1:
        dice_roll_input()
        dice = roll_dice()
        kill_python(dice)


def main():
    """
    Run all program functions
    """
    attack_python()
    

main()
Asked By: Darko Zlatarek

||

Answers:

you can add a while loop.
the while condition: command reapeats the code block if the condition is True and stops on False.

For example the next code will run 3 times and stop one i=4 because i<4 == False

i=0
while i<4:
    print(f"{i=}")
    i+=1

In your case something like this should work

def main():
    """
    Run all program functions
    """
   dice=1 
   while dice==1:
        dice_roll_input()
        dice = roll_dice()
        kill_python(dice)
Answered By: StandingDuck

Good evening. I have found a way to make your code run smoothly when the die lands on a 1. The main issue was in the if statement of the kill_python function, where you only called the dice_roll_input function. The problem with only calling this function is when the user enters roll again, the program only runs the return function and ends since you do not use the other two functions when the dice result is 1. To fully loop through your code after the dice lands on a 1, you need to either call the other two functions separately (in conjunction with the dice_roll_input function) or replace the dice_roll_input function with the "main" function. Either method works fine, but I recommend using the "main" function to condense your code. My rendition of your code can be found below.

from random import randint


def dice_roll_input():
    '''
    Askes the user to type "roll" to run the function that
    simulates rolling the dice.
    '''
    print('Roll more then "1" to kill the python.n')
    dice_conf = input('Type "roll" to roll the dicen')

    if dice_conf == 'roll':
        return
    if dice_conf != 'roll':
        dice_roll_input()


def roll_dice():
    '''
    Simulates rolling the dice and returning the result.
    '''
    random_dice = randint(1,6)
    return random_dice


def kill_python(dice):
    '''
    Based on the dice roll, either python gets killed or
    it dodges the attack and user needs to roll again.
    '''
    if dice == 1:
        print(f'You rolled: {dice} n')
        print('Python dodged your attack. Try again.n')
        main()
    else:
        print(f'You rolled: {dice} n')
        print('You killed the python!')


def main():
    """
    Run all program functions
    """
    dice_roll_input()
    dice = roll_dice()
    kill_python(dice)
    

main()
Answered By: DTcoder

In Python, there are two tools you can use for getting a piece of code to run an unknown number of times.

The first method is the while statement, which looks like this:

while some_logical_value:
    do_some_code()
    do_more_code()
do_other_code()

When the interpreter encounters a while statement, it evaluates whatever value it is given just like an if. The code in the indented block is skipped if the value is considered false, and run if the value is considered true. However, if the code in the indented block is run, the interpreter goes right back to the while statement when it finishes. In the example, once do_more_code() finishes, the interpreter jumps up to the while line rather than continuing to do_other_code() as it would with an if statement.

The second option is something called recursion, which looks like this:

def main():
    do_some_code()
    if some_logical_value:
        main()

Here, we manually instruct the interpreter to go back up to the top of our code. This has its benefits, but it has drawbacks too – it’s typically slower, and has a tendency to use more memory. In addition, Python only lets you go 1000 recursion levels deep before throwing an error by default – so if you have something that naturally exceeds this (like code that should run every second in a game or code to find the 1500th Fibonacci number), you’ll have an issue. While loops, meanwhile, can go on indefinitely.

For more discussion on recursion, see this answer.

Answered By: IntoAMuteCrypt
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.