Below code is working fine locally where as it is failing in hackerrank any modification we need to do ??
n=int(input())
for i in range(n):
for y in range(i 1):
print("* ",end="")
print("")
CodePudding user response:
You are not validating the value that is going into the int()
function.
You can replicate the error with this snippet:
input_value = 'Some String'
int(input_value)
> ValueError: invalid literal for int() with base 10: 'Some String'
In your case I believe you would want to validate the argument provided by the user in input()
.
One alternative would be:
valid_input = False
while not valid_input:
input_val = input()
try:
# Try to cast the value to an int
n = int(input_val)
# If it succeeds save and exit the loop.
valid_input = True
except ValueError:
# If it could not cast it to an int
print('Invalid input -- please enter a number')
And once you have your input, you can proceed with your code:
for i in range(n):
for y in range(i 1):
print("* ", end="")
print("")