[Tutor] Help with function scoping

Richard Mcewan richmcewan at icloud.com
Thu Mar 23 06:15:17 EDT 2017


Thanks Mats, Joel and Alan for helpful advice. 

This code (below) behaves as I wanted now. And think I see where I was going wrong with functions. 

Thank you very much. 

Ps I'll reflect on appropriate variable names also. 

# coding: utf-8
import random

#guess number game

#computer generates random number
def generateNumber():
	computerGuess = int(random.randrange(0,101))
	return computerGuess

#get user
def getUser():
	userGuess = int(raw_input('Guess a number'))
	return userGuess
#call for computer and user
computerGuess = generateNumber()
userGuess = getUser()

#loop to check guess and report
while userGuess != computerGuess:
	if userGuess < computerGuess:
		print('Too low')
		userGuess = getUser()
	elif userGuess > computerGuess:
		print('Too high')
		userGuess = getUser()


#Add counter of guess later. 

Sent from my iPhone

More information about the Tutor mailing list