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
-
Can you add what inputs you used?Jacques Kvam– Jacques Kvam2017年07月26日 04:59:30 +00:00Commented Jul 26, 2017 at 4:59
-
You're comparing a number with a string.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年07月26日 05:01:32 +00:00Commented Jul 26, 2017 at 5:01
2 Answers 2
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
Josh Hamet
9578 silver badges10 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Sagar Chamling
1,0682 gold badges12 silver badges27 bronze badges
Comments
lang-py