-3

Here is my code:

def chose_range():
    while True:
        choice = int(input("Choose the range 100 or 1000:"))
        if choice == 100:
            n = 100
            break
        elif choice == 1000:
            n = 1000
            break
        else:
            print('Error, enter the right number')

        return n

I want to save result of this command into the variable. How can I do this in python 3?

Daniel Yefimov
  • 701
  • 6
  • 20
  • 2
    Are you asking "how do I call a function"? Try `some_variable = chose_range()`. Are you working with a tutorial? Just read ahead a little further and I'm sure it will cover this. – Kevin Aug 29 '16 at 13:35
  • 1
    How does the result differ from your expectation? – syntonym Aug 29 '16 at 13:35
  • You may find this helpful: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – PM 2Ring Aug 29 '16 at 13:38
  • @Kevin I am not so stupid:)Of course i tried some_variable = chose_range(), the problem was that I intented return too much. I didn't catch this misprint. – Daniel Yefimov Aug 29 '16 at 13:43
  • What's the point of `n = 100` and `n = 1000`? You could simply `return choice`. However, your function will crash if the user enters a string that can't be converted to an integer. – PM 2Ring Aug 29 '16 at 13:48

3 Answers3

2

Do not return within your while-loop you break (you have one indent to much).

def chose_range():
    while True:
        choice = int(input("Choose the range 100 or 1000:"))
        if choice == 100:
            n = 100
            break
        elif choice == 1000:
            n = 1000
            break
        else:
            print('Error, enter the right number')
    return n
x = chose_range()
Chickenmarkus
  • 1,073
  • 11
  • 23
0

Did you mean something like

res = chose_range()
Israel Unterman
  • 12,444
  • 3
  • 26
  • 34
0

If you mean "save the result of calling chose_range() into a variable (you didn't say which), then:

theVariable = chose_range()

However, note that your function does not always return a value to be saved; if it hits a break statement, it exits the loop without executing the return statement, at least in how your posted code is indented.

Scott Hunter
  • 46,905
  • 10
  • 55
  • 92