Python loop | Sololearn: Learn to code for FREE!

+12

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 Answer

+9

Daniel 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

+8

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)

+7

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

+4

Simba that’s exactly it!! but yours works and mune doesn’t. Still get the EOF on y=input()

+3

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 :)

+3

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.

+2

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

+2

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 😉....

+2

great thanks!!

+1

would a be the number of iterations?

+1

if yes, then that restriction would be the one that closes the loop, right? what does ++a do? what does a=0 mean?

+1

no, it won’t run and doesn’t recognize neither the ++a not the variables hit or miss

+1

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

+1

Daniel Lopez Check whether the program is properly intended ...

+1

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”:

+1

"miss”

+1

x = 100 a = 0 while a < 4: y = input() if y == "hit": x += 10 else: x -= 20 a = a + 1 still won’t run 😭

+1

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 ...

+1

still gives me the EOF error in line 4 and doesn’t read: y=input ()

+1

in sololearn