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)

1st Jun 2021, 1:26 PM
Adilet Kambarov
Adilet Kambarov - avatar
5 Respostas
0
Thanks guys, all I needed to do is to add break
1st Jun 2021, 5:05 PM
Adilet Kambarov
Adilet Kambarov - avatar
+ 3
Your loop is going infinity
1st Jun 2021, 2:11 PM
Atul [Inactive]
+ 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
1st Jun 2021, 1:42 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 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)
1st Jun 2021, 5:17 PM
visph
visph - avatar
0
Guys please post the right answer of code
12th Sep 2021, 8:34 AM
Makima’s Dog
Makima’s Dog - avatar