I'm kinda new to programming in general, just started to really get into python.
And I'm working on a number guesser project.
import random
def main(): # main function
print("Welcome to the number guesser game")
range_func()
max_guess_number(lower_range_cut, upper_range_cut)
evaluation(random_number, total_guesses)
def range_func(): # allows user to select a range for the number guess
print("Please select a range in which you would like to guess.")
lower_range_cut = int(input("Lower boundary limit: "))
global lower_range_cut
upper_range_cut = int(input("Upper boundary limit: "))
global upper_range_cut
random_number = random.randint(lower_range_cut,upper_range_cut)
global random_number
return lower_range_cut, upper_range_cut, random_number
def max_guess_number(low,high): # returns the total number of guesses
total_numbers = (high - low) + 1
total_guesses = 0
while (2**total_guesses) < total_numbers:
total_guesses += 1
print ("You have a total of %d guesses
"
"for your range between %d to %d"
% (total_guesses, low, high))
global total_guesses
return total_guesses
def evaluation(random_number, total_guesses): # evaluates the users input
guess_count = 0
while guess_count < total_guesses:
user_guess = int(input("Your guess: "))
print("Your guess is: %d" % (user_guess))
if (random_number == user_guess):
print("You got it ")
break
elif user_guess > random_number:
print("Guess lower!")
guess_count += 1
else:
print("Guess higher!")
guess_count += 1
if __name__ == "__main__":
main()
One problem I've experienced while writing that, is that I wasn't able to execute this program without redefining each variables as a global variable. Just by returning the values from one function, I was not able to access e.g. the second returned variable upper_range_cut from the range_function
It there a way to handle that somehow shorter?
Also I'm happy about every note on the code itself (readability, function use, length). I know it could have made this code a lot shorter maybe by using list comprehension, but I don't really have the eye for seeing opportunities in this area yet.
So thanks for any help!
KiliBio
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…