Python Forum
Help turning game into function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help turning game into function (/thread-6573.html)



Help turning game into function - kixxm - Nov-29-2017

#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.


RE: Help turning game into function - metulburr - Nov-29-2017

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


RE: Help turning game into function - micseydel - Nov-29-2017

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.


RE: Help turning game into function - kixxm - Nov-29-2017

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.


RE: Help turning game into function - micseydel - Nov-29-2017

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.


RE: Help turning game into function - kixxm - Nov-29-2017

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.