Python Forum
The count variable is giving me a hard time in this code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The count variable is giving me a hard time in this code
#1
from random import randrange

def toohigh():
	# displays too high sequence
	count += 1
	return('Too high! Try again. ')
	guess = input('Guess my number between 1 and 1000. ')
	play()
	
def toolow():
	# displays too low sequence
	count += 1
	return('Too low! Try again. ')
	guess = input('Guess my number between 1 and 1000. ')
	play()

def correct():
	# displays correct sequence
	count += 1
	return('Congrats! That was the correct number!')
	if count <= 10:
		return('It took you ', str(count), 'guesses.' 'You either know the secret or got lucky.')
	if count > 10:
		return('It took you ', str(count), 'guesses.' 'I think you can do better than that.')
	playagain()

def playagain():
	playagain = input('Would you like to play again? y/n ')
	if playagain == 'y':
		play()
		count = 0
	if playagain == 'n':
		return('Bye!')
		count = 0
	else:
		return('Please answer y or n')
		playagain()

def play():
	# plays game
	count = 0
	x = randrange(1000)
	guess = int(input('Guess my number between 1 and 1000. '))
	while guess != x:
		if int(guess) > x:
			toohigh()
		if guess < x:
			toolow()
		if guess == x:
			correct()

play()
# I am 100% a noob, thank you for helping.
Reply
#2
Quote:The count variable is giving me a hard time in this code
please be a bit more specific
Reply
#3
By default, variables that are assigned inside a function are local to the function. To get data into a function you usually pass the information in as an argument. To get the data out you return() it.

There are other ways, but starting off, this is one of the best.

So for toohigh(), when it gets to line 5, it wants to make count bigger, but it has never been assigned in the function, so it doesn't know what to do.

You should decide whether the functions even need to know the count at all. Maybe you could keep the guess count outside. For instance, your toohigh() and toolow() functions do exactly the same thing except for the text in one line. Probably you could do all the similar stuff elsewhere.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard time trying to figure out the difference between two strings carecavoador 2 675 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,554 Mar-27-2023, 07:38 AM
Last Post: buran
  Row Count and coloumn count Yegor123 4 1,321 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,236 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Clicker count increase by 1 each time blakefindlay 1 5,647 Feb-03-2021, 03:50 PM
Last Post: deanhystad
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,120 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code giving same output no matter the input. Yort 2 2,528 Dec-20-2020, 05:59 AM
Last Post: buran
  Code taking too much time to process ErPipex 11 4,903 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  What is the run time complexity of this code and please explain? samlee916 2 2,289 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  Having a hard time conceptualizing how to print something MysticLord 6 3,096 Sep-19-2020, 10:43 PM
Last Post: MysticLord

Forum Jump:

User Panel Messages

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