Home > Software design >  How do I use a while True loop in this code?
How do I use a while True loop in this code?

Time:02-26

I've just recently started learning Python and I came up with a silly idea just to put in practice some of the things I've learned. My program is supposed to tell the user if the number they typed in is greater than 3. Once the code reachs the end, I want it to loop at the user number input "num=imput("Type number!\n"), line 8, so that I don't have to restart the program every time I want to type in a new number. After a quick Google search I found out I could use the while True loop to do that, but I miserably failed all of my attempts to use it in my program. Can anybody help me out? Here's my code:

print("Is the number you typed greater than 3?")
import time
time.sleep(1)
hi = input("Before we begin, what's your name?!\n")
time.sleep(0.2)
print("You can try anything,", hi, ". Go ahead!")
time.sleep(1)
num=input("Type number!\n") #I want the loop to start here
#
try:
    fnum=float(num)
    if 3<fnum<=100:
        print('Greater than 3')
    elif 100<fnum<=1000:
        print("Definetely greater than 3")
    elif 100<fnum<=10000:
        print("SO MUCH greater than 3!")
    elif fnum>10000:
        print('Youve typed so many digits that you already know, deep in your heart, that this is greater than 3. Take it easy next time', hi,'!')
    elif fnum==3:
        print("Look, I'm no Einstein, but 3 is 3")
    elif 0<=fnum<3:
        print("Less than 3")
    elif fnum<0:
        print("Thats less than zero! Clearly it's less than 3")
    time.sleep(1)
    print("A'll done! :)")
except: 
    print("That's not a number!\n Try again!")

CodePudding user response:

Here is some code with a wile loop, but I also added an exit command, which helps with user-friendliness.

import time
time.sleep(1)
hi = input("Before we begin, what's your name?!\n")
time.sleep(0.2)
print("You can try anything,", hi, ". Go ahead!")
while True: #---------------------------------------While loop here-------------------------
    time.sleep(1)
    num=input("Type number, or 'EXIT' if you wish to quit.\n")#-------------Tell user to type EXIT if he/she wants to
    try:
        fnum=float(num)
        if 3<fnum<=100:
            print('Greater than 3')
        elif 100<fnum<=1000:
            print("Definetely greater than 3")
        elif 100<fnum<=10000:
            print("SO MUCH greater than 3!")
        elif fnum>10000:
            print('Youve typed so many digits that you already know, deep in your heart, that this is greater than 3. Take it easy next time', hi,'!')
        elif fnum==3:
            print("Look, I'm no Einstein, but 3 is 3")
        elif 0<=fnum<3:
            print("Less than 3")
        elif fnum<0:
            print("Thats less than zero! Clearly it's less than 3")
        elif fnum=='EXIT' #---------------------------------------------------Added exit command
            break;
        time.sleep(1)
        print("All done! :)")
    except: 
        print("That's not a number!\n Try again!")

CodePudding user response:

To use the while loop in Python the syntax is very simple. \

    while condition:
      ...

In your case, assuming that everything is fine in your code. \

You can proceed as a follow-up.

...
time.sleep(1)
while True:
    num = input("Type number!\n")  # I want the loop to start here
    ...

You even need a loop stop condition for example we can imagine that if the entry is equal to 0 we leave unless you want an infinite loop.

enter code here
num = input("Type number!\n") 
while float(num):
    ...
    num = input("Type number!\n")

Of course we could continue with while True and then use a break to stop the loop.

    while True:
        num = input("Type number!\n")  # I want the loop to start here
        if not float(num): 
            break
        ...  

Your program does not support input validation so the float(num) Can cause you problems...

  • Related