-1

so from this code, that is writen down below, if k is even i must +3 to K number, if it's odd i must multiply it 5 times, i dont know how to show that if k is even, anyone can help me out?

k=int(input('K= ')
if k%2: # (is this correct? if k is even)
    k+=3
else:
    k*=5
Alex
  • 1,141
  • 8
  • 13
  • 3
    To write Python in the most common standard, also look at [PEP8](https://www.python.org/dev/peps/pep-0008/). It uses 4 spaces for indenting. The [Google Style Guide](https://google.github.io/styleguide/pyguide.html) uses 2 spaces like your answer however. When learning, pick a style guide and stick with it – Ben Oct 12 '16 at 19:04
  • Google is your friend. Try something like "How to check if a number is even in python" – 3ocene Oct 13 '16 at 01:30
  • Possible duplicate of [python - checking odd/even numbers and changing outputs on number size](http://stackoverflow.com/questions/13636640/python-checking-odd-even-numbers-and-changing-outputs-on-number-size) – 3ocene Oct 13 '16 at 01:32
  • Your test is reversed. `k%2` is 0 if `k` is even, and 1 if `k` is odd. Zero is false-ish and one is true-ish, so the `if` block is executed if `k` is odd and the `else` block is executed if `k` is even. Also, you left off the closing parenthesis in `k=int(input('K= ')`. – PM 2Ring Oct 16 '16 at 08:32

2 Answers2

2

I think you should really study some basic books about python.

You see, blocks work by indenting in python; and else needs a ":" too:

if k % 2 == 0:
    k = k+3
else:
    k = k*5

should work better.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2
In python3,

k = int(input())
if k%2 == 0:
    k = k + 3
else:
    k = k * 5

if you are using python2, use raw_input() to take input from the user.

hope this helps.

Rehan Shikkalgar
  • 1,029
  • 8
  • 16