1

I need to stop adding up user inputs when one of them is the string "F". So basically If my input is a int then : += result, if the same input variable is a string then I need to stop and add them together.

My code actually works and has the same inputs and outputs the exercise demands but I'm very unhappy with the way I resolve it.

This is my code:

import numbers
cat = int(input())


def norm(cat):
    res = 0
    for n in range(cat):
      x = int(input())
      res += x

    print(res)

def lon():
    res = 0
    while 2 > 1:
     try :
         y = int(input())
         if isinstance(y,int):
           res +=y
     except:
        print(res)
        break




if cat >= 0 :
    norm(cat)
else:
    lon()

It's actually breaking the while loop in a stupid way by checking if my variable is an int. (I need to make it stop by simply pressing F) Is there any cleaner and shorter way to obtain the same outputs?

Example of the actual inputs-outputs I expect :

in:       out:16    (1 + 3 + 5 + 7)
4
1
3
5
7

in:       out:37     (1 + 3 + 5 + 7 + 21)
-1
1
3
5
7
21
F
5
  • The first thing I'd say is stop saying "dumb" and "stupid". Drop that. You want to look into "scope" for functions and global would be a slippery slope even if it probably looks like a quick fix Commented Sep 25, 2019 at 23:22
  • Just save the input string into a variable s and then check s == 'F' to break out of the loop before converting s to an int. Using try ... except to break out of the loop is also fine IMO. Commented Sep 25, 2019 at 23:24
  • Thank you for your answers. I'm supposed to know nothing about functions or exceptions so the teacher expects from me a total different way of resolving this Commented Sep 25, 2019 at 23:25
  • You execute y = int(input()) and in the next statement if isinstance(y,int): res +=y. If the first statement failed to convert the input to an int, an exception would have been thrown and you never would have fallen through to the second. So isn't that test for y being an int superfluous? Commented Sep 25, 2019 at 23:34
  • RIght, but Stack Overflow isn't a forum, it's supposed to be a repository of specific programming questions and answers. There is no need for self-deprecation - we've all been learners. Beyond that, see this Commented Sep 25, 2019 at 23:36

1 Answer 1

2

You could have written it a bit shorter:

result = 0

while True:
    line = input()
    try:
        result += int(line)
    except ValueError:
        break

print(result)

Notice:

  • import numbers isn't needed. (I didn't even know that existed!)
  • Instead of 2 > 1, you can use True.
  • You don't need to check isinstance(..., int) since int() enforces that.
  • This runs until any non-integer string is reached.

If you want to specifically check for "F" only, it's a bit easier:

result = 0

while True:
    line = input()
    if line == "F":
        break
    result += int(line)

print(result)

Note that without using try, you'll crash the program if you input a non-integer, non-"F" string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.