Python Forum
Help turning game into function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help turning game into function
#1
#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.
Reply
#2
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
Recommended Tutorials:
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,428 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Turning Robot towards goal, comparison of angles SpaceTime 0 2,762 Jan-21-2019, 10:26 PM
Last Post: SpaceTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020