# [Tutor] random number equations . . .

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Jun 3 18:28:13 EDT 2004

```Some comments on general style.

I'd move the newuser() function part further down, it's usually a bad
idea
to mix user selection with processing.

> def newuser():
>     new = raw_input("Are you a new user [y/n]? ")
>     while new not in 'yYnN':
>         new = raw_input("""
>         Please enter 'y' for yes or 'n' for no.
>         Are you a new user? """)
>     if new in 'yY':
newuser()

def newuser():
>         import math
>         vowelcount = 0
>         name = name[:6]
>         namel = namel[:6]
>         name = name + ' ' + namel
>         vowels = ['a','e','i','o','u','A','E','I','O','U']

vowels = "aeiouAEIOU"  # don't need a list

>         for letter in name:
>             if letter in vowels:
>                 vowelcount = vowelcount + 1
>         global startmon
>         global tim

Do these need to be global? If so its probably better practice to
define them outside the function - easier to find in future
- rather than rely on the global statement creating them by
default from within the function.

>         tim = time.time() % 100000
>         startmon = vowelcount * tim
>         startmon = math.sqrt(startmon) * 3141.5962
>         startmon = str(startmon)[:7]
>         startmon = int(float(startmon))

This duplicates the code for newacid below, so you could make it
into a function:

def makeCodedValue(seed):
global tim  # I'll assume you really do mean this...
val = seed * tim
val = math.sqrt(startmon) * 3141.5962
val = str(startmon)[:7]
val = int(float(startmon))
return value

Then call it like:
startmon = makeCodedValue( vowelcount )

and
newacid = makeCodedValue( birthday )

Then if you change the algorithm its only done in one place.

>         print "Welcome, %s.  You have \$%d to use in BankSim 1.0." %
(name,
> startmon)

Much the same applies to newacc():

> def newacc():

move this down...

>     global newac

Does this need to be global, I don't think its used anywhere
else? Global variables are usually something to avoid if possible.

>     newac = raw_input("Would you like to create a new account [y/n]?
")
>     while newac not in 'yYnN':
>         newac = raw_input("""
>         Please enter 'y' for yes or 'n' for no.
>         Would you like to create a new acount [y/n]? """)
>     if newac in 'yY':
newacc()

def newacc():
>         import math
>         global newacid
[ddmmyyyy]. "))
>         newacid = newacc( birthday )
>         initbal = int(raw_input("How much money would you like to
put in your
> account? "))
>         print "Account number %d created with \$%d in it." %
(newacid, initbal)
>         mon = startmon - initbal
>         print "You now have \$%d in account %d and \$%d out of the
bank." %
> (initbal, newacid, mon)

Then I'd put all the early user input stuff with the while loops in
either two separate functions or a single function, depending on how
you expect to use them.

Even better would be to try to move all of the user input stuiff
outside
and have the functions only do the calculations, that way it will be
much easier if you decide to write a GUI inteface someday.

Just some thoughts,

Alan G.

```