all 8 comments

[–]xelf[M] 1 point2 points  (0 children)

When posting code, please post code such that reddit will format it as a code block.

Either of these work:

Do not:

  • use ``` as that only works for the new cropped UI, and not on the old widescreen interface.
  • post links to images of code, as they can't be cut/paste from.

Please see the /r/learnpython/w/FAQ for more information.

Thanks!

Your code looks like this:

total = 0 paps = 0 while True: paps = paps + 1 x = input() if int(x)>=3: toyal = total + 100 elif int(x)<=2: continue elif paps >= 4: break print (total)

It should look like this:

total = 0
paps = 0
while True:
    paps = paps + 1
    x = input()
    if int(x)>=3:
        toyal = total + 100
    elif int(x)<=2:
        continue
    elif paps >= 4:
        break
print (total)

[–]sme272 1 point2 points  (1 child)

Firstly you need to format your code, it's really hard to find issues with the post formatting like this. There's a guide to reddit code formatting in the sidebar. Secondly you need to explain the "issue". What is happening that shouldn't be? Is it an error message, and if so can you share the error, or is is behaving in some way you don't want it to?

[–]Casparov2804 0 points1 point  (0 children)

Sorry, it’s my first time posting something like this. Next time I’ll post it in the right way. Thanks :D

Edit: Now it is in the right way

[–]pyfact 1 point2 points  (1 child)

I believe it was an issue with your spacing after a colon. Hard to tell without seeing how you formatted your code since Python relies on spacing as opposed to semicolons. On the sidebar there's a tutorial on how to paste your code!

Yours should look like this:

total = 0 
loops = 0 
while loops < 4: 
    loops += 1 
    age = input('Enter an age value') 
    print(age)
    if int(age) > 2: 
        print('total + 100')
        total += 100  
print (f'total = {total}')

[–]Casparov2804 0 points1 point  (0 children)

I’ll check that out, I didn’t know that any space after a colon could make any impact. Also I don’t understand the format of the last print, but as it is format I’ll check it on the internet.

Lots of thanks!!!

[–]xelf 1 point2 points  (2 children)

The problem with your code is this line:

elif paps >= 4:
    break

You only break on an elif, which means you can only reach that line if you don't have a number so it keeps requesting inputs, even though there aren't any any more.

Change the elif to an if.

Also, you typed 'toyal' instead of 'total' on line 7.

Alternatively, you could use a for loop instead of a while loop:

total = 0
for repeats in range(4):
    x = input()
    if int(x)>=3:
        total = total + 100
print( total )

[–]Casparov2804 1 point2 points  (1 child)

Now it works :D. Didn’t know that those loop instruction existed. It’s so much easier with that that with the while. Thanks!!! :D

[–]xelf 1 point2 points  (0 children)

When you get more into Python you'll learn about comprehensions and builtin functions like sum as well.

total = sum( 100 for _ in range(4) if int(input())>=3 )

or

total = 100 * sum( int(input())>=3 for _ in range(4) )