0

PYTHON: A little confused on while loops...specifically using input (Pull the trigger challenge)

I’m attempting to do the while challenge on the python beginners course. The problem reads as follows: 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 What am I using in the while? I know I need to use an if and else statement for input()=“hit” and “miss”(respectively) and have them either add or subtract the 10/20. However I seem to have issues determine what should be written in the while loop so that it ends appropriately. Any assistance would be terrific thank you.

5th Feb 2021, 6:15 PM
Karsa9Fingers
Karsa9Fingers - avatar
6 odpowiedzi
+ 3
shots_left = 4 score = 100 while shots_left > 0: # get the input # use if-else to check for hit or miss # add to score if hit subtract if miss # deduct 1 from shots_left # after loop output score Don't over complicate it. Sometimes the easiest solution that meets the requirements is the best solution, because readability and time spent writing the code matter too! You can always come back to your code later and refactor it etc. P.S. You could also use shots_left = 4 ... while shots_left: ... ... ... shots_left -= 1 when shots_left is equal to 0, which is falsy, the loop will exit. This might be a bit more Pythonic.
5th Feb 2021, 7:59 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Ok, then you can use a while i<4 and increment i in each iteration But there has to be a cleaner solution... (using a 'count' from the itertools module for example)
5th Feb 2021, 6:26 PM
Angelo
Angelo - avatar
+ 1
ChaoticDawg thank you again sir!! I am still getting to used to “thinking in python” and find myself overcomplicating things. Your continued assistance is greatly appreciated.
6th Feb 2021, 12:43 PM
Karsa9Fingers
Karsa9Fingers - avatar
0
It would be easier using a for loop
5th Feb 2021, 6:19 PM
Angelo
Angelo - avatar
0
Angelo Agreed but the challenge requires a “while” loop
5th Feb 2021, 6:20 PM
Karsa9Fingers
Karsa9Fingers - avatar
0
Angelo Thats the problem im having. im no python expert but im re-learning it after a hiatus, and im trying to only use the tools provided by the python for beginners course
5th Feb 2021, 7:04 PM
Karsa9Fingers
Karsa9Fingers - avatar