my question , Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s. For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x" that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.) . My code:
def generate_n_chars(c, n):
d = ""
c = raw_input("Give a character: ")
n = raw_input("Give a word: ")
for i in n:
if i not in c:
d += i
return d
print ("You word have " + str(generate_n_chars('c', 'n') + " character"))
so, when I write hallo a come only a x , and I will five , because hallo have five words.I will to come xxxxx but WITHOUT '5 * x' .Very Thanks for yours help!
The question is asking you to create a function(def
) named generate_n_chars()
that takes an integer(int
) value,n
and character(char
) value,c
as parameters. Here, c
is any character input by user,lets say user inputs x
and n
is how many times the character(c
) should be printed so lets say user wants it to print 5 times and therefore inputs 5
. Then the result would be xxxxx
. 5 x's.
def generate_n_chars(n, c):
result = ""
for i in range(n):
result += c
return result
inputChar = input("Enter the character:")
inputNum = int(input("Enter the number of times " + str(inputChar) + " to be printed:"))
print("Result: " + str(generate_n_chars(inputNum, inputChar)))
Output:
Enter the character:x
Enter the number of times x to be printed:5
Result: xxxxx