Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

Can I exit from loop without using 'break' or without using variable set to 'true'

I am going through a book (python crash course) and doing exercises. I have a problem when inside a code, for a certain part I need to set variable set to string and then change it to integer.

Here is the attempt which brings error ( I need a help with this one) :

prompt = " Tell me your age : "
prompt += "
(Once finished type 'quit')"
#define variable and set to 'nothing' so python has something to compare
    info_from_user = ""
# loop while the variable is not 'quit'
while info_from_user != 'quit':
  info_from_user = input(prompt)
  info_from_user = int(info_from_user)
  if info_from_user < 3:
    print("It's FREE for you")
  elif info_from_user <= 12:
      print("Please pay 10$")
  elif info_from_user > 12:
      print("Please pay 15$")

Successful examples, only for your reference and full understanding:

**1. using variable set to 'True' i.e. we set a variable named 'active' and set it to 'True'. The look while active: **

    prompt = "
 Tell me your age : "
prompt += "
(Once finished type 'quit')"
#define variable and set in True
active = True
# apply loop that will be active as long as active is true using if/else
while active:
    info_from_user = input(prompt)

# condition to stop the loop active if user inputs quit
    if info_from_user == 'quit':
        active = False
# else change variable to integer so python can compare user input with numbers.        
    else:
        info_from_user = int(info_from_user)
        if info_from_user < 3:
            print("It's FREE for you")
        elif info_from_user <= 12:
            print("Please pay 10$")
        elif info_from_user > 12:
            print("Please pay 15$")

2. using break , i.e. when user inputs 'quit' then break

prompt = "
 Tell me your age : "
prompt += "
(Once finished type 'quit')"
#define empty variable so python got something to check against
info_from_user = ""
while info_from_user != 'quit':
info_from_user = input(prompt)


if info_from_user == 'quit':
    break
    
else:
    info_from_user = int(info_from_user)
    if info_from_user < 3:
        print("It's FREE for you")
    elif info_from_user <= 12:
        print("Please pay 10$")
    elif info_from_user > 12:
        print("Please pay 15$")
question from:https://stackoverflow.com/questions/65924346/can-i-exit-from-loop-without-using-break-or-without-using-variable-set-to-tru

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, without some kind of flow control you cannot exit loops, you need some conditions to be met in order to exit a loop. The reasons why the first examples throws an error are

  1. At line 4 indentation is incorrect, and in python indentation does affect the compiling process, so a correct indentation is fundamental

  2. You cast info_from_user to int, which is needed for you to make comparisons in your if statements below. But if you cast the data to int, then obviously you can't input quit because that is clearly not an integer.

My suggestion is instead of saying once finished type 'quit', you say once finished type '-1', which is a flag value you'll use to let the program understand you want to quit

prompt = " Tell me your age : "
prompt += "
(Once finished type '-1')"
#define variable and set in True
info_from_user = 0
# apply loop that will be active as long as active is true using if/else
while info_from_user != -1:
  info_from_user = input(prompt)
  info_from_user = int(info_from_user)
  if info_from_user > -1:
    if info_from_user < 3:
      print("It's FREE for you")
    elif info_from_user <= 12:
        print("Please pay 10$")
    elif info_from_user > 12:
        print("Please pay 15$")

There are of course more refined options to make this, using the 'quit' string aswell, but as for right now I don't have the brains to come up with a different, more polished solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...