You can do it like this:
n = input('Digite o texto todo em maiúscula: ')
while True:
if n.isupper():
print('Texto correto')
break
else:
n = input('Texto errado, digite tudo em maiúscula: ')
while True
creates a loop infinite, which is only interrupted by the break
- which in turn only happens if n.isupper()
return True
. That is, if n.isupper()
for True
, he prints the message and leaves the while
.
Note that if a value is boolean (as is the return of isupper
), do not need to compare it with True
or False
, just put it directly as condition of the if
.
In your case, if res != True
could be written as if not res
, as well as if res == True
may simply be if res
, but actually this variable res
nor is it necessary.
If n.isupper
return False
, he falls in the else
and asks you to type the text again.
Just remembering that isupper
checks that all calls cased characteres of the string are uppercase, and there must be at least one of these in the string.
In the case, cased characters are those belonging to the Unicode categories "Letter, Uppercase", "Letter, Lowercase" and "Letter, Titlecase". But this does not mean that the string only contains these characters. For example, if the string is "1A"
or "A, B"
, then isupper
also returns True
(the digit "1", the comma and the space are not cased characters, so are not checked).
Could you describe this detail better using the prefix
f
to use flash memory and not RAM? For the prefixf
defines that the string can be interpolated, because it impacts the memory that will be used?– Woss
When you print a string, it goes into the RAM to be printed. However, when you put the f in front of the string, it stops going to the RAM and goes to the flash memory (faster) and is later printed on the screen. And that’s why the code gets faster when you put f in front of the string in print !!!
– Matheus Teixeira
But what is the relation of f-strings to flash memory? Where did you read that this happens? There is the documentation link that states this?
– Woss
Dude, I saw it in a video class about 2 or 3 years ago, when I was programming more in Arduino... Then, when I went to learn Python, I could use that too! I don’t know why, but it works!!!
– Matheus Teixeira
Python does not work like that. The prefix f is to inform the interpreter that the string will be interpolated. It has no connection to the memory used.
– Woss