0

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()
5
  • What is your expected output, and what output are you getting instead? You said you have a problem getting the name but don't say what the problem is. I suspect the issue is that irepnum is < 1 when you call inamed, so the loop in that function will never end. It's good practice to use a less than condition for loops like that, to prevent potential out of range bugs like this (use while count < irepnum instead, and initialize count to 0) Commented Nov 26, 2013 at 20:13
  • my fault, I got an infinite loop when I ran the code Commented Nov 26, 2013 at 20:32
  • I can read that. You're going to have to be more descriptive. Commented Nov 26, 2013 at 20:34
  • I want the name entered by the user to be displayed the number of times the user enters a number, so if a user enter the name "jeff" and the number "6" I want "jeff" to be printed on the screen 6 times. Sorry for the odd wording. Commented Nov 26, 2013 at 20:41
  • I can read that as well, I meant more descriptive about the problem you are having. Things like what inputs you give, what outputs you get, etc. Be specific, don't just say "I got an infinite loop". It looks like your question was answered anyways, but keep that in mind for future questions. Commented Nov 26, 2013 at 20:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer, I'm moving all of this data from a flowchart tool that has a different syntax than python, I didn't keep track of all my variables, but thanks for correcting that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.