Python loop
How to get to add with the loop some points: x = 100 hit = x + 10 while x: if hit: print (x) x += 10 elif: print (x) x -= 20
2/18/2021 2:45:56 AM
Daniel Lopez
34 Answers
New AnswerDaniel Lopez this way EOF is recognised as end of input, also allowing you to write as many lines as you want: x = 100 try: while True: if input() == "hit": x += 10 else: x -= 20 except: print(x) NOTE: This works on SoloLearn only, normally doing so you would have an infinite input loop
According to the task, Your 'a' should start from 1. Also, you missed to print the final score. Daniel Lopez Lol I missed to notice one thing. We have to change our while loop condition. It should be while a<5 So, your code needs to be , x= 100 a=1 while a <5: y= input() if y == "hit": x+=10 elif y== "miss": x-=20 a+=1 print(x)
Daniel Lopez & Dipanshu Kubde This is how multiple inputs are handled in sololearn ide. miss (Enter) miss (Enter) hit (Enter)... https://code.sololearn.com/ciavU58GXz1D/?ref=app Btw, I think you solved this code challenge. https://www.sololearn.com/post/664632/?ref=app
Daniel Lopez Yes đ a is no of iteration a=0 means it initialize with 0 and ++a will increase its value up to 4 You can ask for input from the user If in the question it is mentioned Otherwise not you can solve EOF by try and except method
Simba I got the answer Thank you, Simba And Daniel Lopez See the Simba last comment You know what the problem you face Pin his comment if you get the answer so that other can see It Solves my
Simba thatâs exactly it!! but yours works and mune doesnât. Still get the EOF on y=input()
Daniel Lopez Question unclear and the if condition will always be true and thus theres no role for an elif after that ... Pls clarify the question with sample input and output for recieving better help :)
Daniel Lopez the EOF (End Of File) exception is thrown when there is no more data to pass to input(). The "try" block checks exception raising, "except" is the block called when any exception is thrown. I used "while True" because we don't necessarily know how many lines of input the user will write, whatever the number will be we're sure that the process will ends and outputs the result when input ends.
youâre right! sorry!! the question is how to calculate the final score of a player that beguins with 100 points and takes a shot. if he misses, he looses 20 points. if he hits, he adds 10 points. example imput: miss miss hit miss expected output: 50
Try : x=100 a=0 while(a<4): y=input("hit or miss") if y=="hit ": x+=10 else : x-=20 ++a print(x) //no of iterations not mentioned in the question , considering it to be 4 from sample input đ....
if yes, then that restriction would be the one that closes the loop, right? what does ++a do? what does a=0 mean?
no, it wonât run and doesnât recognize neither the ++a not the variables hit or miss
Daniel Lopez While Loop iterates 4 times ie from a=0 to a=3 and when "a" becomes 4 ,the loop terminates ... ++a => a=a+1 its a prefix increment operator ...You can change the while condition to suit your needs ....hope it helps
it does help. i managed to fix the iteration and the ++a. however, the program throws a EOF error when I put y = input (âhitâ or âmiss) so that the if condition recognizes the variable hit in: if y == âhitâ:
x = 100 a = 0 while a < 4: y = input() if y == "hit": x += 10 else: x -= 20 a = a + 1 still wonât run đ
You need to provide all the inputs at once in sololearn ,it is not interactive like visual Studio or any other IDE Dipanshu Kubde Lol what you saying ? Pls dont forward misleading answers ...