+ 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)
14 Antworten
+ 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
+ 2
Your input statement should be in the while loop also
+ 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 - -
+ 1
I see now! Thank you Rik. Now it makes sense. And thanks everyone else, too!
+ 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)
0
Thanks Caleb - indeed I meant i - = 1
Still having that problem though! But thanks for pointing that out!
0
KRae what language is this in?
0
Python - it's one of the exercises in Python beginners
0
Thanks for the reply Addy. I'm very confused as to how to do that. 😞
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)
0
actionGet.php
0
My problem is that I can’t get the score to print
0
Hi, did you manage to figure out what the problem was with your code? I'm having issues with this same task