I'm new to Python and learning. I try to write a code
My issue when I cant re-call the input with my code (with duplicated input) or break the loop (after new name)
list=["MT", "Smith Jenkins", "PT", "CP"]
while True:
user=input("Your full name :")
usercheck="".join(user.split())
print(usercheck)
for i in list:
j="".join(i.split())
if usercheck ==j:
print("Duplicated ! Please enter new name")
else:
print("Thanks")
break
Thank you.
You need to think about that
So you can use the for/else
construction, the else
will be executed if no break
has been seen in the loop, in your case if no duplicated has been found, there you'll be able to break the while loop
names = []
while True:
user = input("Your full name :")
usercheck = "".join(user.split())
print(usercheck)
for name in names:
j = "".join(name.split())
if usercheck == j:
print("Duplicated ! Please enter new name")
break
else:
print("Thanks")
break
If that is too tricky, you can keep with a variable that will help you know whether you've seen a duplicate or not
while True:
user = input("Your full name :")
usercheck = "".join(user.split())
duplicated = False
for name in names:
j = "".join(name.split())
if usercheck == j:
print("Duplicated ! Please enter new name")
duplicated = True
break
if duplicated:
print("Thanks")
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.