Python, Calling a function again based on input from user

RaulG

I'm trying to get back to the roll_die function within take_turn function after the user enters "y", then continue adding value to turn_score variable. Can anyone see what I'm doing wrong? Thanks in advance, here's my code so far:

import random
def roll_die():
    roll_value = random.randint(1,6)
    return roll_value

def take_turn(is_human_turn):
    roll_die()
    roll = roll_die()
    turn_score = 0
    turn_score = turn_score + roll

    if is_human_turn:
        print("total so far:",turn_score)
        input("roll again? y/n ")
        if input == "y":
            roll_die()
        # Trying to get back to the roll_die func w/in take_turn func
        #  and continue adding value to turn_score

    return turn_score

take_turn(True)
Laurent H.

I suggest you to:

  1. Gather the 3 first lines of take_turn() function into turn_score = roll_die()
  2. Use while loop to roll again until user enters something different from "y"
  3. Add the value returned by roll_die() to turn_score
  4. Do something with the value returned by take_turn() (otherwise do not return anything)

Here is my full code solution:

import random

def roll_die():
    roll_value = random.randint(1,6)
    return roll_value

def take_turn(is_human_turn):
    turn_score = roll_die()
    while is_human_turn:
        print("total so far:", turn_score)
        if input("roll again? y/n ") == "y":
            turn_score += roll_die()
        else:
            is_human_turn = False
    return turn_score

result = take_turn(True)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a function recursively for user input

From Dev

Calling a function recursively for user input

From Dev

Call a function in a child component based on an input from user in Angular

From Dev

Calling function from input variable

From Dev

Execute a function with an instance based on user input (in conjunction with a class) [Python]

From Dev

Calling an Excel user defined function from Python always returns None

From Dev

Calling a function with a variable input in Python

From Dev

Calling stored function using user input variable

From Dev

Calling angularjs function on text input based on length

From Dev

Python - efficiently select from list based on user input

From Dev

Python: Create list of user-defined methods from a module and referencing/calling them by user input

From Dev

Python - input from a user

From Dev

prevent a function running multiple times after calling it again from ajax

From Dev

How to keep running the code in main file after calling the function to get input value from user in R programming

From Dev

Calling a function based on argument of previous function in Python

From Dev

How do I get input from user and call a function based on the input?

From Dev

testing a function which takes input from user (python)

From Dev

testing a function which takes input from user (python)

From Dev

cannot read input while calling from function

From Dev

Change 'Var' function of assignin based on user input

From Dev

Calling user defined function from AngularJS directive

From Dev

Python calling function in an function from another function

From Dev

Calling C function from Python

From Dev

Python: Calling a function based on a argument value

From Dev

Use user input within as input in a function Python

From Dev

Let the user input the name again

From Dev

Select data from DB based on user input

From Dev

Remove Object from ArrayList based on User Input

From Dev

calling class with user input

Related Related

  1. 1

    Calling a function recursively for user input

  2. 2

    Calling a function recursively for user input

  3. 3

    Call a function in a child component based on an input from user in Angular

  4. 4

    Calling function from input variable

  5. 5

    Execute a function with an instance based on user input (in conjunction with a class) [Python]

  6. 6

    Calling an Excel user defined function from Python always returns None

  7. 7

    Calling a function with a variable input in Python

  8. 8

    Calling stored function using user input variable

  9. 9

    Calling angularjs function on text input based on length

  10. 10

    Python - efficiently select from list based on user input

  11. 11

    Python: Create list of user-defined methods from a module and referencing/calling them by user input

  12. 12

    Python - input from a user

  13. 13

    prevent a function running multiple times after calling it again from ajax

  14. 14

    How to keep running the code in main file after calling the function to get input value from user in R programming

  15. 15

    Calling a function based on argument of previous function in Python

  16. 16

    How do I get input from user and call a function based on the input?

  17. 17

    testing a function which takes input from user (python)

  18. 18

    testing a function which takes input from user (python)

  19. 19

    cannot read input while calling from function

  20. 20

    Change 'Var' function of assignin based on user input

  21. 21

    Calling user defined function from AngularJS directive

  22. 22

    Python calling function in an function from another function

  23. 23

    Calling C function from Python

  24. 24

    Python: Calling a function based on a argument value

  25. 25

    Use user input within as input in a function Python

  26. 26

    Let the user input the name again

  27. 27

    Select data from DB based on user input

  28. 28

    Remove Object from ArrayList based on User Input

  29. 29

    calling class with user input

HotTag

Archive