I'm trying to do so that when I insert my name it checks if it has numbers in it (success) and if the name is to long. And once I type the name again it checks it all again. it works with checking the numbers, but it doesn't work with checking if it's to long. Instead, it just continues the code.
print('Hi! Whats your name?')
def nome():
global pontos
def hasNumbers(nomezito):
return any(char.isdigit() for char in nomezito)
print(nome + 'has numbers')
def longName(longevidade):
return len(nome) < 3 or len(nome) > 13
nome = input().title()
number = hasNumbers(nome)
long = longName(nome)
while number == True or long == True:
if number == True:
print('A name cant have any numbers. Please tell me your real name')
nome = input().title()
number = hasNumbers(nome)
continue
elif long == True:
print('Your name is too long to be real. Please tell me your real name.')
print(longName(nome))
nome = input().title()
long = longName(nome)
continue
PS: I translated it from portuguese to english so you can read it better, but I might have made some mistakes. nome()
If the user enters a name with digits, the code enters the if number == True:
block. Then when the user enters another name, you calculate number = hasNumbers(nome)
, but you don't run nameLongo
again. So long
is still referencing whether the previous name is too long. You need to call nameLongo
in both branches, and hasNumbers
in both branches.
print('Name:')
def nome():
global pontos
def temNumeros(nomezito):
return any(char.isdigit() for char in nomezito)
print(nome + 'tem numeros')
def nomeLongo(longevidade):
return len(nome) < 3 or len(nome) > 13
nome = input().title()
number = temNumeros(nome)
long = nomeLongo(nome)
while number == True or long == True:
if number == True:
print('digits.')
nome = input().title()
number = temNumeros(nome)
long = nomeLongo(nome)
continue
elif long == True:
print('Too long.')
print(nomeLongo(nome))
nome = input().title()
number = temNumeros(nome)
long = nomeLongo(nome)
continue
nome()
Result:
Name:
123
digits.
ffffffffffffffffffffffffffffffffffffffffffffff
Too long.
True
kevin
... But ultimately I think it would be easier to call input
exactly once in the loop, rather than in each branch of the conditional.
def contains_digits(name):
return any(char.isdigit() for char in name)
def is_wrong_length(name):
return len(name) < 3 or len(name) > 13
while True:
name = input("Name: ")
if contains_digits(name):
print("Name can't contain digits.")
elif is_wrong_length(name):
print("Name must be between 3 and 13 characters.")
else:
#name is valid, so exit the loop
break
print("Welcome,", name)
Result:
Name: 123
Name can't contain digits.
Name: ffffffffffffffffffffffffffffffffffffffffff
Name must be between 3 and 13 characters.
Name: Kevin
Welcome, Kevin
def longName(longevidade):
return len(longevidade) < 3 or len(longevidade) > 13
you were not putting 'longevidade' as the arguments of len()
EDIT : Here is your problem you were also not updating the value of number and long
print('Hi! Whats your name?')
def nome():
global pontos
def hasNumbers(nomezito):
return any(char.isdigit() for char in nomezito)
print(nome + 'has numbers')
def longName(longevidade):
return len(longevidade) < 3 or len(longevidade) > 13
nome = input().title()
number = hasNumbers(nome)
long = longName(nome)
while number == True or long == True:
if number == True:
print('A name cant have any numbers. Please tell me your real name')
nome = input().title()
number = hasNumbers(nome)
long = longName(nome)
continue
elif long == True:
print('Your name is too long to be real. Please tell me your real name.')
print(longName(nome))
nome = input().title()
long = longName(nome)
number = hasNumbers(nome)
continue
nome()
If a while loop didn't play again that means the statement you given for it isn't correct
Your statement is
while number == True or long == True:
so it only ran once because the second run after the loop all of your statements
number == True
and this statement
long == True
is not working . What I suggest is you run each statement separately and see which one is working this way you can solve the issue
or create function for each like
def number():
if number == True:
print('A name cant have any numbers. Please tell me your real name')
nome = input().title()
number = hasNumbers(nome)
else:
pass
def long():
if long == True:
print('Your name is too long to be real. Please tell me your real name.')
print(longName(nome))
nome = input().title()
long = longName(nome)
else:
pass
while True:
number()
long()
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.