Number Guess Problem


#1

what is the point of making function one instead of making just one function to solve a problem

i mean if we delete function one and change the variable guess of the second function to be like the function one we will get the same result
just one guess like this
guess = int(input("Guess a number: "))


#2

and less code is always better? I would do the following:

def get_user_guess():
    return int(raw_input("Guess a number: "))

def roll_dice(number_of_sides):
    guess = get_user_guess()

now your function have separate tasks, making the code easier to maintain and test (unit test).

actually, i would do the following:

def get_user_guess():
    return int(raw_input("Guess a number: "))

def roll_dice(user_guess, number_of_sides):
    print "to do"

def main():
   guess = get_user_guess()
   roll_dice(guess, 6)

main()

#3

that’s mean i am able to delete function one and make change of function 2

but we did it just like that because the whole question needs that :smiley:


#4

but its also important to understand why.

guess = int(raw_input("Guess a number: ")) can fail when the user doesn’t enter an integer:

image

by placing this code in a separate function we can write a unit test for this isolate piece of code.

Organizing code is important:

https://medium.com/@msandin/strategies-for-organizing-code-2c9d690b6f33


#5

thank you for helping me