+ 1
How can I manage the exclamation mark in this code
Eg:- If there are 4 u then the exclamation mark should be 1 Ratio = 4:1 https://sololearn.com/compiler-playground/cu49MgjZUy55/?ref=app
7 odpowiedzi
+ 5
Rain ,
when we have 8 `u`, we should get 8//4 => 2 exclamation marks .
your code sample shows 3 of them.
this means, that we have to handle the case when number of `u` is <= 4 different.
sample:
def siuuuuuu_generator(n):
if isinstance(n, int) and n > 0:
u_string = 'u' * n
return 'si' + u_string + '!' if n <= 4 else 'si' + u_string + (n // 4) * '!'
else:
return 'Invalid input. Please enter a positive integer.'
num = int(input())
print(siuuuuuu_generator(num))
+ 4
P A Arrchith Iyer ,
to get the output according your definition, we can use an if / else conditional or a ternary:
> you could use an if conditional for all input numbers <= 4 to add / concatenate just one '!'.
> in the else clause you can generate the new multiplicator for '!' by diividing the input number by 4 and make it an int value.
> use this number to multiplicate '!' and create the output string for the else condition..
+ 4
Rain ,
thanks, i forgot the second slash, so division should be integer division. already corrected.
+ 1
Jay Matthews what if we enter the number that less than 4
+ 1
Lothar ,
That's why I said it was "close". However, after this mod, it's perfect.
for n in range(1, 17):
print(
"si"
+ "u" * n
+ "!" * (((n - 1) // 4) + 1)
)
""" Output:
siu!
siuu!
siuuu!
siuuuu!
siuuuuu!!
siuuuuuu!!
siuuuuuuu!!
siuuuuuuuu!!
siuuuuuuuuu!!!
siuuuuuuuuuu!!!
siuuuuuuuuuuu!!!
siuuuuuuuuuuuu!!!
siuuuuuuuuuuuuu!!!!
siuuuuuuuuuuuuuu!!!!
siuuuuuuuuuuuuuuu!!!!
siuuuuuuuuuuuuuuuu!!!!
"""
There are two problems with yours. The first is you can't multiply str * float. The second (with repair, and looped) is your ranges aren't the same width, so the progression isn't smooth. In other words, your first range is 7 values wide.
""" Output:
siu!
siuu!
siuuu!
siuuuu!
siuuuuu!
siuuuuuu!
siuuuuuuu!
siuuuuuuuu!!
siuuuuuuuuu!!
siuuuuuuuuuu!!
siuuuuuuuuuuu!!
siuuuuuuuuuuuu!!!
siuuuuuuuuuuuuu!!!
siuuuuuuuuuuuuuu!!!
siuuuuuuuuuuuuuuu!!!
siuuuuuuuuuuuuuuuu!!!!
"""
0
P A Arrchith Iyer ,
# This is close.
for n in range(1, 11):
print(
"si"
+ "u" * n
+ "!" * ((n // 4) + 1)
)
""" Output:
siu!
siuu!
siuuu!
siuuuu!!
siuuuuu!!
siuuuuuu!!
siuuuuuuu!!
siuuuuuuuu!!!
siuuuuuuuuu!!!
siuuuuuuuuuu!!!
"""
0
Glad you found the solution! It's easy to overlook a simple detail like a missing second slash, but rectifying it ensures the intended integer division. Great job on the correction!