Python Forum

Full Version: a little help with a While Loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A newbie here Big Grin

This code prints "too many tires" at every loop.
The goal is to print it at the end of the loop only. (after 3 wrong tries)
Can someone help ?

x = 1
while x <= 3:
    name = input("Enter your username: ")
    if len(name) < 5:
         print(f"{name} is too short")
    elif len(name) > 9:
         print (f"{name} is too long")
    else:
         print(f"Welcome {name} !")
         break
    x += 1
    print("too many tries !")
I figured it

x = 1
while x <= 3:
    name = input("Enter your username: ")
    if len(name) < 5:
         print(f"{name} is too short")
    elif len(name) > 9:
         print (f"{name} is too long")
    else:
         print(f"Welcome {name} !")
         break
    x += 1
else:
    print("too many tries !")
(Feb-07-2020, 04:25 PM)Hob_78 Wrote: [ -> ]I figured it

x = 1
while x <= 3:
    name = input("Enter your username: ")
    if len(name) < 5:
         print(f"{name} is too short")
    elif len(name) > 9:
         print (f"{name} is too long")
    else:
         print(f"Welcome {name} !")
         break
    x += 1
else:
    print("too many tries !")
if i give a name which is having length of 7, it is printing welcome msg why ???