Python Forum

Full Version: Help turning game into function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#Make a guess and get feedback.
guess=0
fb=0
while fb != 'c' or fb != 'C':
guess = random.randint(lower, upper)
print("I'm guessing your number is", guess, ". ")
fb=input("Type H if too high, L if too low, or C if correct. ")
if fb == 'l' or fb == 'L':
print("My guess was too low! ")
lower = guess + 1
elif fb == 'h' or fb == 'H':
print("My guess was too high! ")
upper = guess - 1
else:
print("Your entry was invalid. ")
#If guess is correct.
if fb == 'c' or fb == 'C':
print("------------------------------------------")
print("\tHooray! I guessed right! ")
print("------------------------------------------")



I need help turning this entire code above into a function.

sorry about the indentation. It got lost with posting it.
Quote:sorry about the indentation. It got lost with posting it.
thats because you need to use python tags as explained in the BBCode link in my signature
What have you tried, solve your problem? Do you just want to know how functions work, or do you have a specific question? If the former, check the tutorials section.
I have a basic understanding of how functions work but I can't figure out to make this one block of code a function. I tried to define it just as is but I'm getting syntax errors.
Well you can turn any code into a function by indenting it and putting the def header above it. If you have a specific attempt, you should post it, since there's not much help we can provide without seeing the exact problem-code.
I actually just figured it out. My lack of sleep must be getting to me. I tried to call it without passing arguments that were needed.