0

hi there i'm learning python i like to know if this script can be better or shorter

import sys
g = 1
def trying():
    q = input('enter (y or yes) to retry')
    if not q == 'y' or q == 'yes':
        return 0
while g == True:
    try:
        t = int(input('please enter an integer:'))
        r = t % 2
        if r == 0:
            print('your number is even')
            if trying() == 0:
                g = 0
        else:
            print('your number is odd')
            if trying() == 0:
                g = 0
    except ValueError:
        print('sorry you need to enter numbers only')

3 Answers3

2

If you want in shorter, here is my version..

while True:
    try:
        print('Your number is %s' % ('even' if int(input('Please enter an integer: ')) % 2 == 0 else 'odd'))
        if  input('Enter (y or yes) to retry: ') not in ['y', 'yes']: break

    except ValueError:
        print('Sorry you need to enter numbers only')

What you want here is a do-while loop. You can easily implement it by adding a break statement to a infinite while-loop. More on this topic can be found here.

Next thing, you have to add a try-except statement as there is a string to integer conversion is happening.

print('Your number is %s' % ('even' if int(input('Please enter an integer: ')) % 2 == 0 else 'odd'))

This statement will return "Your number is even" if the input is even else it will return "Your number is odd". This method is called python ternary operator.
Then you can wrap it with a print-function to print the returned string. Look here.

input('Enter (y or yes) to retry: ') not in ['y', 'yes']

This check whether the user input is not there in the given list. So if user input is neither "y" or "yes", while-loop will break.

Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
0

Here is an example of how the code can be made simpler. Remember that code should rarely be repeated. In most cases, if you have repeating lines of code it can be simplified.

while True:
    try:
        t = int(input('please enter an integer:'))
        if t % 2 == 0: print('your number is even')
        else: print('your number is odd')

        q = input('enter (y or yes) to retry')
        if not (q == 'y' or q == 'yes'): break

    except ValueError:
        print('sorry you need to enter numbers only')
JahKnows
  • 2,618
  • 3
  • 22
  • 37
  • Little tip if you're going for shorter code: don't declare a variable to use once. Just define it in the spot! Eg: replace t with the input line. Also there doesn't have to be a line break after a colon. – Levi Lesches Mar 26 '18 at 04:50
  • I think there is an important trade-off between shortened code, and readable code. – JahKnows Mar 26 '18 at 04:59
0
def trying():
    question = input('enter (y or yes) to retry')
    if not (q == 'y' or q == 'yes'):
        return 1
    return 0
while True:
    try:
        num1 = int(input('please enter an integer:'))
        num2 = t % 2
        if not num2:
            print('your number is even')
        else:
            print('your number is odd')
        if trying():
            break
    except ValueError:
        print('sorry you need to enter numbers only')

You don't have to import sys in your program as you didn't used it and you don't have to. You don't have to store anything in a variable for a while loop. Just assigning True and breaking out will do. If you're looking for anything that is True (this includes an unempty list, string, and dictionaries; and numbers not equal to 0). You should make if var:. If the variable evaluates to True. The conditional block will be executed. This is a clearer syntax so it is recommended. Name your variables with words, not with letters. This will not make your code longer and it will make your code better.

This is all I can do with your code. If there is more, please state them.