+ 2

Pull the trigger

I'm completely new to this and I just can't figure this one out. My code just adds the first value 4 times instead of the 4 different inputs. I'm sure I'm making some pretty silly mistake and I'd be very grateful if anyone could point me in the right direction and explain what I'm doing wrong, please! result = input("") x = 100 # score i = 4 # shots_left while i > 0 : if result == "hit" : x = x + 10 else: x = x - 20 i = -1 print (x)

10th Feb 2021, 1:32 AM
KRae
KRae - avatar
14 Respostas
+ 6
KRae I am not familiar with the exercise or the expected output, but your code may need to look like this. x = 100 # score shots = 4 # shots_left while shots > 0 : result = input() if result == "hit" : x += 10 else: x -= 20 shots -=1 print (x) As per previous suggestions: 1. Repaired -= operator 2. Emplaced input into while loop so the loop will keep requesting a new result 3. Renamed i to shots , cosmetic only, because it indicates number of shots, i tends to indicate iteration
10th Feb 2021, 1:58 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Your input statement should be in the while loop also
10th Feb 2021, 1:42 AM
♡Addy♡
♡Addy♡ - avatar
+ 1
i dont know if this is your only problem but you have i = -1 i am pretty sure you mean i -= 1 or i - -
10th Feb 2021, 1:37 AM
Brain & Bones
Brain & Bones - avatar
+ 1
I see now! Thank you Rik. Now it makes sense. And thanks everyone else, too!
10th Feb 2021, 2:06 AM
KRae
KRae - avatar
+ 1
Here is my code x = 0 i = 100 aHit = 10 aMiss = 20 while(x < 4): shot = input() if(shot == "hit"): i = i + aHit x += 1 #counter inside if statement elif(shot == "miss"): i = i - aMiss x += 1 #counter inside if statement print (i)
26th Aug 2021, 2:38 AM
Alex Sands
Alex Sands - avatar
0
Thanks Caleb - indeed I meant i - = 1 Still having that problem though! But thanks for pointing that out!
10th Feb 2021, 1:40 AM
KRae
KRae - avatar
0
KRae what language is this in?
10th Feb 2021, 1:41 AM
Brain & Bones
Brain & Bones - avatar
0
Python - it's one of the exercises in Python beginners
10th Feb 2021, 1:42 AM
KRae
KRae - avatar
0
Thanks for the reply Addy. I'm very confused as to how to do that. 😞
10th Feb 2021, 1:54 AM
KRae
KRae - avatar
0
KRae i dont know if that is it but to put it in the while loop do this. result = input("") x = 100 # score i = 4 # shots_left while i > 0 : if result == "hit" : x = x + 10 else: x = x - 20 i -=1 print (x)
10th Feb 2021, 1:55 AM
Brain & Bones
Brain & Bones - avatar
0
actionGet.php
12th Feb 2021, 12:14 AM
hasan devil
hasan devil - avatar
0
My problem is that I can’t get the score to print
27th Apr 2021, 1:36 AM
Chris
Chris - avatar
0
Hi, did you manage to figure out what the problem was with your code? I'm having issues with this same task
28th Apr 2021, 5:52 PM
Aindrea Van Rheede
Aindrea Van Rheede - avatar