You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if question.startswith("-") and question[1:].isdigit():
18
+
return-int(question[1:])
19
+
elif question.isdigit():
20
+
returnint(question)
17
21
18
22
found_op =False
19
23
for name, op inOPS.items():
@@ -56,8 +60,9 @@ The method calls are [chained][method-chaining], so that the output from one cal
56
60
If the input has no characters left,
57
61
it uses the [falsiness][falsiness] of an empty string with the [`not`][not] operator to return a `ValueError("syntax error")`.
58
62
59
-
Next, the [`isdigit`][isdigit] method is used to see if the remaining characters in the input are digits.
60
-
If so, it uses the [`int()`][int-constructor] constructor to return the string as an integer.
63
+
Next, the [`str.startswith()`][startswith] and [`isdigit`][isdigit] methods are used to see if the remaining characters in the input are either negative or positive digits.
64
+
Because "-" is used to denote negative numbers, `str.startswith("-")` is used in the first condition and `question[1:].isdigit()` is then used for the remaining string.
65
+
If the `str.isdigit()` checks pass, the [`int()`][int-constructor] constructor is used to return the string as an integer with the proper sign.
61
66
62
67
Next, the elements in the `OPS` dictionary are iterated over.
63
68
If the key name is in the input, then the [`str.replace`][replace] method is used to replace the name in the input with the `dunder-method` value.
@@ -94,5 +99,6 @@ When the loop exhausts, the first element of the list is selected as the functio
0 commit comments