+ 2

Python pull the trigger

EDIT: I’ve made progress but again I’m stuck Here is the question I’m stuck on You are making a game! The player tries to shoot an object and can hit or miss it. The player starts with 100 points, with a hit adding 10 points to the player’s score, and a miss deducting 20 points. Your program needs to take 4 action results as input ("hit" or "miss"), calculate and output the player’s remaining points. Sample Input hit hit miss hit Sample Output 110 Explanation: 3 hits add 30 points, one miss deducts 20, making the total points equal to 110. Here is my code so far it seems to work on Pycharm on my laptop but not quite right, definitely doesn’t pass the the little quiz on the app though score = 100 tries = 4 while tries >= 0: if input() == "miss": print("miss") score -= 20 print(score) if input() == "hit": print("hit") score += 10 print(score) tries -= 1 I feel like I’m missing something obvious, again a dumbed down push in the right direction is so helpful

6th Feb 2021, 8:56 PM
Spencer Imeson
Spencer Imeson - avatar
20 Réponses
+ 19
Hey James, youre pretty close! just need your input() variable inside of your while loop. You probably also need tries to be greater than (but not equal to) 0. here is an example of my working code: shots = 4 score = 100 while shots > 0: result = input() if result == "hit": score +=10 shots -=1 elif result == "miss": score -=20 shots -=1 print(score)
11th Feb 2021, 4:32 PM
Drew Roberts
Drew Roberts - avatar
+ 5
Hey Carolina, imagine this is a script and we’re only running through the code one time. If that’s the case and the input was outside of the loop, we would get input one time and the while loop would take that single input and use it every time it loops. If we put the input variable inside the while loop, we are accepting new input multiple times in the script since the while loop keeps asking for additional input. I hope that helps make it make sense, TL;DR is just that we need the variable to change each time the while loop runs.
16th May 2021, 4:01 PM
Drew Roberts
Drew Roberts - avatar
+ 4
I fixed my code after Drew Roberts comment saying that the input variable should be inside the while loop, but I can't see why it's like that... Why it couldn't be outside the while loop?
13th Mar 2021, 3:38 PM
Gabriel Coelho
Gabriel Coelho - avatar
+ 4
Ohh i got it now! Thank you very much Drew :)
16th May 2021, 4:40 PM
Carolina Rodrigues Arangues
Carolina Rodrigues Arangues - avatar
+ 3
if input() is equal to "hit" then add 10 to score otherwise add 20 to score if input is equal to "miss"
6th Feb 2021, 9:19 PM
Abhay
Abhay - avatar
+ 3
Try this bruh shots = 1 score = 100 while shots <= 4 : atau = input() if atau == "miss" : score = score - 20 shots += 1 else : score = score + 10 shots += 1 print (score)
20th Mar 2021, 1:22 AM
Endry Saputra
+ 1
Could someone explain why the input should be inside while loop, please? Thanks :)
16th May 2021, 3:49 PM
Carolina Rodrigues Arangues
Carolina Rodrigues Arangues - avatar
+ 1
Hello James I'm here to help you and you should put the variable I = 1 or no according you want to either variable or 1 directly and do not forget the count even the program doesn't print to infinite. score = 100 tries = 4 i = 1 while i<=tries: result = input("Enter hit or miss: ") if result == 'hit': print("hit") score += 10 print("score = " + str(score)) elif result == 'miss': print("miss") score -= 20 print("score = " + str(score)) i += 1 output: Enter hit or miss: hit hit score = 110 Enter hit or miss: hit hit score = 120 Enter hit or miss: hit hit score = 130 Enter hit or miss: hit hit score = 140
22nd May 2021, 4:04 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
+ 1
sum = 0 while True: x = input() if x == "stop": break else: sum += int(x) print(sum)
24th Aug 2021, 11:16 AM
Engin Baştürk
Engin Baştürk - avatar
+ 1
my working code points=100 action=1 while action <=4: result =input() action+=1 if result == "hit": points+=10 if result == "miss": points-=20 print(points)
6th Sep 2021, 1:30 PM
Ioana Serban
0
Heyo, not sure if you already got this answered but i just wanted to make sure you knew that you’re only supposed to print the score once at the end of the loop. Fixing that along with making one of those if statements an elif (like Abhay hinted at) should do the trick!
8th Feb 2021, 9:19 PM
Drew Roberts
Drew Roberts - avatar
0
Here's my code score = 100 action = input() #4 tries tries = 4 while tries >=0: if action == "miss": score += 20 tries -= 1 elif action == "hit": score += 10 tries -= 1 print(score) For some reason everytime the output is 150 and I can't for the life of me work out why. I'm sure it's something simple....
11th Feb 2021, 11:26 AM
James Peppitt
James Peppitt - avatar
0
Thank you so much for that. I have been pulling my hair out for ages.
11th Feb 2021, 4:37 PM
James Peppitt
James Peppitt - avatar
0
here is mine: (ik its diffrent but it works rly well!!:)) sco = 100 sho = 1 while sho <= 4: if input() == “hit”: print(sco + 100) else: print(sho - 20) sho+=1
3rd Mar 2021, 1:40 AM
NERA77
NERA77 - avatar
0
I cant do it please help me.
27th May 2021, 8:12 PM
267 Janenisa Benjathikul
267 Janenisa Benjathikul - avatar
0
My Correct code: points=100 x=1 while x<5: my_input=input() if my_input=="hit": points +=10 elif my_input=="miss": points -=20 x=x+1 print(points)
27th May 2021, 10:48 PM
truqoddess
truqoddess - avatar
0
Guy's want to post my code here and it works perfect: points = 100 x = 1 while x <= 4: my_input = input() x = x + 1 if my_input == "hit": points += 10 elif my_input == "miss": points -= 20 print(points)
23rd Sep 2021, 9:19 PM
Sergei Tiulenev
Sergei Tiulenev - avatar
0
points_start = 100 hit_points = 10 miss_points = -20 i = 1 action = str(input('Enter action result: hit or miss. ')) while i <= 4: if action == 'hit': i += 1 points_start = points_start + hit_points print(points_start) action = str(input('Enter action result: hit or miss. ')) if action == 'miss': i += 1 points_start = points_start + miss_points print(points_start) action = str(input('Enter action result: hit or miss. ')) else: print ('Number of shots exceeded')
11th Dec 2021, 8:28 PM
Marcelo Goes de Freitas
Marcelo Goes de Freitas - avatar
0
As to why input should be in the while, this is as you are to receive 4 inputs and this requires 4 input() statements. You need the while loop to generate the input() / action = input () 4 times automatically Just like this :- action1 = input() If action1 == "hit": .. .. action4 = input() If action4 == "hit": .. .. But this: while i <= 4: action = input() #action1 - 4 .. replaces it. (Note :- 1. I did not complete the code as the other parts/logic has been in other posts/comments - you could just fill it in the dotted (..) positions 2. If you must put input outside the loop it must be four inputs or must split an imputed text of four text if any i.e the user must type the four "hits" / "misses" at once.)
16th Jan 2023, 5:07 AM
Oluwatobiloba Abraham
Oluwatobiloba Abraham - avatar
- 1
shots = 4 score = 100 while shots > 0: result = input() if result == "hit": score +=10 shots -=1 elif result == "miss": score -=20 shots -=1 print(score)
8th May 2021, 8:55 AM
Ashok Swami
Ashok Swami - avatar