while True:
_group = input("")
if type(_group)==int:
print("number")
breake
elif type(_group)==str:
print("word")
breake
elif type(_group)==bool:
print("value")
breake
else:
continue
_________________________________________________________________
doesn't work :(
different inputs
It doesn´t work because you wrote "break" with an e at the end.
It would work if the indentation is correct which we can´t see as you didn´t use the editor function </> to insert code.
If your code looks like above it works but will always output "word" as input() always yields a string.
It would work if the indentation is correct which we can´t see as you didn´t use the editor function </> to insert code.
Code: Alles auswählen
while True:
_group = input("")
if type(_group) == int:
print("number")
break
elif type(_group) == str:
print("word")
break
elif type(_group) == bool:
print("value")
break
else:
continue
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
- __blackjack__
- User
- Beiträge: 13122
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
@Kasper: Further comments: One leading underscore in local names usually means that this name gets assigned some value that isn't used. But `_group` *is* used, so that's a bit confusing.
The empty string is the default value for `input()`\s `prompt` argument. And the ``else`` with ``continue`` doesn't have an effect because that is what happens anyway.
As __deets__ already mentioned `input()` returns strings. But if you actually would need to test for a value's type, the `isinstance()` function would be preferable as it also recocnizes subtypes. Then the order of the tests have to be different as `bool` is a subtype of `int`:
So the code would look like this:
Now theres three times almost the same code for each type. This can be replaced by a loop over the differences:
The loop(s) are left with ``return`` now, because ``break`` would just leave the inner loop.
This leaves us with the problem that `group` is a string. Always. So let's forget about the code with the ``while`` loop, because if `group` is not a string describing a boolean value or a number, it is a ”word”:
The empty string is the default value for `input()`\s `prompt` argument. And the ``else`` with ``continue`` doesn't have an effect because that is what happens anyway.
As __deets__ already mentioned `input()` returns strings. But if you actually would need to test for a value's type, the `isinstance()` function would be preferable as it also recocnizes subtypes. Then the order of the tests have to be different as `bool` is a subtype of `int`:
Code: Alles auswählen
In [32]: isinstance(True, bool)
Out[32]: True
In [33]: isinstance(True, int)
Out[33]: True
In [34]: isinstance(0, bool)
Out[34]: False
In [35]: isinstance(0, int)
Out[35]: True
Code: Alles auswählen
while True:
group = input()
if isinstance(group, bool):
print("value")
break
if isinstance(group, int):
print("number")
break
if isinstance(group, str):
print("word")
break
Code: Alles auswählen
#!/usr/bin/env python3
def main():
while True:
group = input()
for type_, text in [(bool, "value"), (int, "number"), (str, "word")]:
if isinstance(group, type_):
print(text)
return
if __name__ == "__main__":
main()
This leaves us with the problem that `group` is a string. Always. So let's forget about the code with the ``while`` loop, because if `group` is not a string describing a boolean value or a number, it is a ”word”:
Code: Alles auswählen
#!/usr/bin/env python3
def main():
group = input()
if group in ["True", "False"]:
print("value")
elif group.isdigit():
print("number")
else:
print("word")
if __name__ == "__main__":
main()
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Hello,
Why do I need a name when I dosen't use them?
Thank you.
Dennis
can you tell me please a example when to use one leading underscore?__blackjack__ hat geschrieben: ↑Samstag 9. Oktober 2021, 14:04 @Kasper: Further comments: One leading underscore in local names usually means that this name gets assigned some value that isn't used.
Why do I need a name when I dosen't use them?
Thank you.
Dennis
"When I got the music, I got a place to go" [Rancid, 1993]
I'm sure __blackjack__ has a good comment on that as well, but since I had my hands already on the keyboard:
It becomes obvious to the reader, that "president" is split into two parts. However only the "lastname" is needed in the following code section.
"_first" is only used as a container when unpacking parts of the "president" string.
A bit more ugly and less readable approach would be this:
So, it's about readability.
Also, some code checkers will issue a warning, that a value is assigned to "first" without ever accessing it. However, with the leading underscore, this warning is silenced.
Code: Alles auswählen
presidents = [
"Joe Biden",
"Donald Trump",
"Barack Obama",
"George Bush",
]
for president in presidents:
_first, lastname = president.split(" ")
print(lastname)
"_first" is only used as a container when unpacking parts of the "president" string.
A bit more ugly and less readable approach would be this:
Code: Alles auswählen
for president in presidents:
lastname = president.split(" ")[-1]
print(lastname)
Also, some code checkers will issue a warning, that a value is assigned to "first" without ever accessing it. However, with the leading underscore, this warning is silenced.
Hello rogerb,
thank you for your explanation.
I think if I had to write your example, my code would be ugly. So it's good to know to use the underscope.
Greetings
Dennis
thank you for your explanation.
I think if I had to write your example, my code would be ugly. So it's good to know to use the underscope.
Greetings
Dennis
"When I got the music, I got a place to go" [Rancid, 1993]
- __blackjack__
- User
- Beiträge: 13122
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Another example are arguments of functions or methods that need to be there, for instance because the base class method expects them or because they are used as callbacks and get called with some argument(s). For instance `bind()` callbacks from `tkinter` pass in an event object but often that isn't used. A typical signature of such a callback method would be:
Code: Alles auswählen
def on_mouse_click(self, _event=None):
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman