0
Shooting Game Python
code should take 4 inputs and output points in the end. My code works fine when I try in my local IDLE, but Sololearn doesn't make it pass the test. Please help what's wrong: #Code takes "hit" or "miss" as inputs --> if hit (+10 points), if miss (-20 points) x = 100 #x is initial points count = 0 while True: usr = input() if count < 4: #code should take 4 inputs so made to count to 4 if usr == "hit": x += 10 elif usr == "miss": x -= 20 count +=1 if count == 4: print(x)
5 Respuestas
0
Thanks guys, all I needed to do is to add break
+ 3
Your loop is going infinity
+ 2
Hi! for you, the loop condition will always be true and will be executed forever, since you have not figured out and written how to get out of it. make a better exit condition the number of shot attempts
+ 1
# to run a code 4 times, you rather should use a 'for' loop:
for c in range(4):
print("loop count", count)
print("loop exited count", count)
# output:
"""
loop count 0
loop count 1
loop count 2
loop count 3
loop exited count 3
"""
# almost same with 'while':
count = 0
while count < 4:
print("loop count", count)
count += 1
print("loop exited count", count)
# output:
"""
loop count 0
loop count 1
loop count 2
loop count 3
loop exited count 4
"""
# notice that only the last line differ ;)
# similar with infinite loop:
count = 0
while True:
if count < 4:
print("loop count", count)
count += 1
else:
print("count", count)
break
print("loop exited count", count)
# output:
"""
loop count 0
loop count 1
loop count 2
loop count 3
count 4
loop exited count 4
"""
# without 'break' statement inside the 'else' statement, loop never exit, so line 'count 4' will be outputed forever (program should be interupted if run locally, or timeout in sololearn)
0
Guys please post the right answer of code