+ 1
Python Pull the Trigger 25.1
#your code goes here score = 100 shot = 1 i = input() while shot <= 4: if i == "hit": score + 10 shot += 1 elif i == "miss": score + -20 shot =+ 1 print (score) I can’t understand why my output is 100 on the first problem use case in which the input is as follows: hit miss hit miss Upon running my code output is 100 And this is after I edited the code multiple times finding errors and slimming the code each time, as far as I understand it should be 80??? What am I missing here ???
6 Réponses
0
Your score variable is never changed. Replace
score + 10
in the if block with
score += 10
and replace
score +- 20
in the elif block with
score -= 20
You also have to put the input statement inside the while statement or you will have only one input.
0
Simon Sauter it's answer cover all points but also,in elif statement
You write shot =+1 instead write
shot +=1
0
score = 100
shot = 0
while shot <= 4:
i = input()
if i == "hit":
score += 10
shot += 1
elif i == "miss":
score -=20
shot += 1
print (score)
So here is my updated code and yet the output now is :
Traceback (most recent call last):
File "/usercode/file0.py", line 5, in <module>
i = input()
EOFError: EOF when reading a line
I really appreciate your help, patients and understanding, with me on this, I have a bad habit of needing to understand in depth why something does or doesn't work, Im working on changing my career and come from an HVAC/mechanical services background
0
https://code.sololearn.com/cs977n5taMEJ/?ref=app
Vicum Velit See this you put the variable i = input() in wrong place that's why you got the problem
You have to place it when all variables are declared don't put it in functions like if , while.etc
See this code And understand it And if any problems in understanding ask me in messages
And also I updated your code
I also included else in it because when you someone put integer in then it will come no output that's why always put else statement at last
Happy Coding!
0
See I copied and pasted that code for the exercise and still not getting the correct output (now 150)
score = 100
shot = 0
i = input()
while shot <= 4:
if i == "hit":
score += 10
shot += 1
elif i == "miss":
score -=20
shot += 1
print(score)
The first test case inputs are:
hit
miss
hit
miss
Says that the output should be 80
The second test cast has inputs of:
miss
miss
miss
hit
My output is 0
it says the output should be 50
Im so lost cause by my current understanding the code should work, yet its not and im tempted to just copy and paste the correct answer listed in discussions, however I just can not see why my coder is not working
compared to this code which is listed and runs, I just can't see why mine does not
score = 100
I = 0
while I<4:
shot = input()
if shot=="hit":
score+=10
elif shot=="miss":
score-=20
I+= 1
print (score)
or is it that sololearn requires the answers to be coded exact to the answers shown in code section ??
I just can't understand and im not going to be satisfied till I know why my output is not working so I know not make the same mistake in the future if I am.... once again I greatly appreciate your time and help as I look forward to being able to do the same for others in the future once I can
0
Hi Vicum!
In your updated code, there is an some extra space found before elif and variable I. You can just remove it and see the result.
Here it is your working code
score = 100
I = 0
while I<4:
shot = input()
if shot=="hit":
score+=10
elif shot=="miss":
score-=20
I+= 1
print (score)