Posts: 279
Threads: 107
Joined: Aug 2019
Oct-05-2023, 07:50 PM
(This post was last modified: Oct-05-2023, 07:51 PM by Mark17.)
Here's a program from an intro book:
x = input("Enter some input for identification: ")
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric"
elif x.isspace():
r = "space-based"
elif x.isascii():
r = "ASCII text"
else:
r = "a mystery"
print("x is " + r) The program is entered in a single Jupyter Notebook cell. When I run the cell and paste the input 987f4, the output seems to be alphabetical or ASCII depending on the previous run. If the previous run was all letters, then the current run will output "alphabetical." Same goes for "ASCII" [with input being shift + number keys to get symbols like &*%(*^ ) ].
Why is this?
Also, when I tried this the first several times, I entered a combination of numbers and letters and got output that was my input--the same combination of numbers and letters. It wasn't one of the strings assigned in the if-elif-else branches. Any idea what happened there?
Posts: 12,025
Threads: 484
Joined: Sep 2016
x will always be a string.
you can convert to whatever is needed.
Example:
orig = input("enter a float")
print(f"orig: {orig}, type orig: {type(orig)}")
afloat = float(orig)
print(f"afloat: {afloat}, type afloat: {type(afloat)}") Output: enter a float: 123.45
orig: 123.45, type orig: <class 'str'>
afloat: 123.45, type afloat: <class 'float'>
Posts: 6,780
Threads: 20
Joined: Feb 2020
Oct-05-2023, 08:48 PM
(This post was last modified: Oct-05-2023, 08:49 PM by deanhystad.)
This logic is wrong:
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric" A str can be alphanumeric and not be numeric or alphabetical.
def test(x):
print("isalnum", x.isalnum())
print("isalpha", x.isalpha())
print("isnumeric", x.isnumeric())
print()
test("a")
test("2")
test("a2") Output: isalnum True
isalpha True
isnumeric False
isalnum True
isalpha False
isnumeric True
isalnum True
isalpha False
isnumeric False
In your code, entering "a2" results in nothing assigned to r. In regular python you would get a name error.
x = "a1"
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric"
elif x.isspace():
r = "space-based"
elif x.isascii():
r = "ASCII text"
else:
r = "a mystery"
print("x is " + r) Error: Traceback (most recent call last):
File "...", line 14, in <module>
print("x is " + r)
NameError: name 'r' is not defined
In the notebook, it just uses the last value assigned to r. Sort of like this :
for x in ("a", "a1", "1", "a1", " ", "a1"):
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric"
elif x.isspace():
r = "space-based"
elif x.isascii():
r = "ASCII text"
else:
r = "a mystery"
print(x, "is", r) Output: a is alphabetical
a1 is alphabetical
1 is numeric
a1 is numeric
is space-based
a1 is space-based
"a1" is pretty versatile.
I do not know how r could be assigned the input string, Maybe some bizarre convenience feature? If you fix your logic the program will work as expected.
Posts: 279
Threads: 107
Joined: Aug 2019
Oct-06-2023, 12:43 PM
(This post was last modified: Oct-06-2023, 12:43 PM by Mark17.)
(Oct-05-2023, 08:41 PM)Larz60+ Wrote: x will always be a string.
you can convert to whatever is needed.
Example:
orig = input("enter a float")
print(f"orig: {orig}, type orig: {type(orig)}")
afloat = float(orig)
print(f"afloat: {afloat}, type afloat: {type(afloat)}") Output: enter a float: 123.45
orig: 123.45, type orig: <class 'str'>
afloat: 123.45, type afloat: <class 'float'>
This exercise is intended to illustrate nested if statements. Thanks for reminding me about f-strings. I'll have to review that.
For future posts, how do I include the output as you did in this response?
Posts: 279
Threads: 107
Joined: Aug 2019
Oct-06-2023, 01:27 PM
(This post was last modified: Oct-06-2023, 01:27 PM by Mark17.)
(Oct-05-2023, 08:48 PM)deanhystad Wrote: This logic is wrong:
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric" A str can be alphanumeric and not be numeric or alphabetical.
def test(x):
print("isalnum", x.isalnum())
print("isalpha", x.isalpha())
print("isnumeric", x.isnumeric())
print()
test("a")
test("2")
test("a2") Output: isalnum True
isalpha True
isnumeric False
isalnum True
isalpha False
isnumeric True
isalnum True
isalpha False
isnumeric False
In your code, entering "a2" results in nothing assigned to r. In regular python you would get a name error.
x = "a1"
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric"
elif x.isspace():
r = "space-based"
elif x.isascii():
r = "ASCII text"
else:
r = "a mystery"
print("x is " + r) Error: Traceback (most recent call last):
File "...", line 14, in <module>
print("x is " + r)
NameError: name 'r' is not defined
In the notebook, it just uses the last value assigned to r. Sort of like this :
for x in ("a", "a1", "1", "a1", " ", "a1"):
if x.isalnum():
if x.isalpha():
r = "alphabetical"
if x.isnumeric():
r = "numeric"
elif x.isspace():
r = "space-based"
elif x.isascii():
r = "ASCII text"
else:
r = "a mystery"
print(x, "is", r) Output: a is alphabetical
a1 is alphabetical
1 is numeric
a1 is numeric
is space-based
a1 is space-based
"a1" is pretty versatile.
I do not know how r could be assigned the input string, Maybe some bizarre convenience feature? If you fix your logic the program will work as expected.
I'm pretty shocked to see the difference between how JN handles the name error (by using last value assigned, instead)! One thing that's been consistent in my learnings of Python over the years is _inconsistency_... that I will run a program multiple times and get different results (like this). I've always used either JN or Spyder. This would explain for JN. I guess if it happens in the future (inconsistent output), I'll have to somehow try to debug for that.
The book uses Visual Studio Code whereas I'm sticking with JN. At first, I thought that was to blame for a seemingly sloppy exercise included in a commercial publication. However, this isn't just an IDE issue is it? Didn't they err on the very categorization of alphabetical vs. alphanumeric vs. numeric?
Thanks Dean! Nice to see you're a Mod now. You've always been extremely helpful.
Posts: 6,780
Threads: 20
Joined: Feb 2020
Oct-06-2023, 05:06 PM
(This post was last modified: Oct-06-2023, 05:06 PM by deanhystad.)
Jupyter is kind of like a spreadsheet. Changing the results produced by a cell may change the results produced by other cells, which in turn may change the results in the original cell. That's kind of the whole point. Like a spreadsheet, it is a "What if?" tool. "If I change how this is done, how does it affect everything else?"
Are you sure you copied the example correctly? Your code has a big hole in the logic. Maybe that was the purpose of the example, to identify the hole?
Posts: 279
Threads: 107
Joined: Aug 2019
Oct-06-2023, 06:39 PM
(This post was last modified: Oct-06-2023, 06:40 PM by Mark17.)
(Oct-06-2023, 05:06 PM)deanhystad Wrote: Are you sure you copied the example correctly? Your code has a big hole in the logic. Maybe that was the purpose of the example, to identify the hole?
This is the first and only example in the section "Create Nested if Statements," p.141 of _Teach Yourself Visually Python_ by Guy and Ted Hart-Davis (2022). Step 10 says "type your choice of input and then press Enter." To me, that means input anything and I tried a combination of letters and numbers.
|