Reputation: 107
I can't seem to get user input for a number of times to display. For example, if the input is
Jeff 6
The output should be
Jeff
Jeff
Jeff
Jeff
Jeff
Jeff
I'm new to functions in Python, but here is my code thus far:
def getName():
name = raw_input("please enter name")
return name
def getRepval():
irepnum = float(raw_input("please enter number to show name entered"))
return irepnum
def inamed(name, irepnum):
count = 1 #the loop to show the name entered by the user
while irepnum != count:
print name
count += 1 #do I need to use return??
def main(): #having the main func like this gives me an infinite loop
irepnum = 0
iname = getName() #I think my problem is somewhere here.
irepnum = getRepval()
inamed(irepnum,name)
main()
Upvotes: 0
Views: 120
Reputation: 154916
You need to call inamed(iname, irepnum)
, not inamed(irepnum, name)
, as you are doing now.
Other than the obvious mistake of name
not being defined (the actual variable is called iname
), the wrong order causes irepnum
in the function to be set to a string the user entered as name. Since count
, no matter how large, never compares equal to the passed string, the code loops infinitely.
Several tips:
Learn to use the for
loop and xrange
. The idiom you want is for count in xrange(irepnum):
. (Using it would have prevented this bug.)
Give more distinctive names to your identifiers. Currently you have an inamed
function and an iname
variable. Confusing.
Don't use floats where an int
would suffice. Misusing floats is asking for trouble.
Upvotes: 1