[Tutor] Help with scoping

Richard Mcewan richmcewan at icloud.com
Thu Mar 23 13:38:13 EDT 2017


Hi

Thanks Alan and all. 

I've incorporated your points. I've explored how values are returned and used by functions. I switched to Python to check a Swift3 bug and I think I can see a number of issues more clearly now. 

I put the count and call for next user guess back a tab at the end of the while loop instead of duplicating at the end of if conditions. 

Appreciate the help. Will come back to the list with new topics in the future no doubt and look forward to mailings. 

Thanks Richard

Code from 2.7 Python running on Pythonista iOS app. 

# coding: utf-8
import random

#guess number game

#number to guess
def generateNumber():
	someValue = int(random.randrange(0,101))
	return someValue #sic


#get user guess
def getUser():
	userGuess = int(raw_input('Guess a number: '))
	return userGuess
	
#loop to check guess and report
def main():
	count = 0
	
	#call for hidden number and user 1st guess
	hiddenNumber = generateNumber()
	userGuess = getUser()
	
	#main loop to report on guess
	while userGuess != hiddenNumber:
		if userGuess < hiddenNumber:
			print('Too low\n')
		elif userGuess > hiddenNumber:
			print('Too high\n')
		#note new position
		count +=1
		userGuess = getUser()
	
	print('Correct! ' , count+1, 'guesses')

main()

#future: deal with bad input. 
#invite user to set size of game.
#Range of fun messages reported. 
#Record best scores. 




More information about the Tutor mailing list