0
def generate_n_chars(n,s="."):
 res=""
 count=0
 while count < n:
 count=count+1
 res=res+s
 return res
print generate_n_chars(raw_input("Enter the integer value : "),raw_input("Enter the character : "))

I am beginner in python and I don't know why this loop going to infinity. Please someone correct my program

Ivan Kolesnikov
1,8151 gold badge31 silver badges46 bronze badges
asked Jul 26, 2017 at 4:56
2
  • Can you add what inputs you used? Commented Jul 26, 2017 at 4:59
  • You're comparing a number with a string. Commented Jul 26, 2017 at 5:01

2 Answers 2

1

The reason is because the input will be evaluated and set to a string. Therefore, you're comparing two variables of different types. You need to cast your input to an integer.

def generate_n_chars(n,s="."):
 res=""
 count=0
 while count < n:
 count=count+1
 res=res+s
generate_n_chars(int(raw_input("Enter the integer value : ")),raw_input("Enter the character : "))
answered Jul 26, 2017 at 5:02
Sign up to request clarification or add additional context in comments.

Comments

1
def generate_n_chars(n, s = "."):
 res = ""
 count = 0
 while count < n:
 count = count + 1
 res = res + s
 return res
print generate_n_chars(input("Enter the integer value : "), raw_input("Enter the character : "))

Here input("Enter the integer value : ") input instead of raw_input

raw_input() => https://docs.python.org/2/library/functions.html#raw_input

input() => https://docs.python.org/2/library/functions.html#input

answered Jul 26, 2017 at 5:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.