How do I check if a string is a negative number before passing it through int()?

2024/12/9 22:08:58

I'm trying to write something that checks if a string is a number or a negative. If it's a number (positive or negative) it will passed through int(). Unfortunately isdigit() won't recognize it as a number when "-" is included.

This is what I have so far:

def contestTest():
# Neutral point for struggle/tug of war/contest
x = 0while -5 < x < 5:print "Type desired amount of damage."print xchoice = raw_input("> ")if choice.isdigit():y = int(choice)x += yelse:print "Invalid input."if -5 >= x:print "x is low. Loss."print x
elif 5 <= x:print "x is high. Win."print x
else:print "Something went wrong."print x

The only solution I can think of is some separate, convoluted series of statements that I might squirrel away in a separate function to make it look nicer. I'd be grateful for any help!

Answer

You can easily remove the characters from the left first, like so:

choice.lstrip('-+').isdigit()

However it would probably be better to handle exceptions from invalid input instead:

print x
while True:choice = raw_input("> ")try:y = int(choice)breakexcept ValueError:print "Invalid input."
x += y
https://en.xdnf.cn/q/72933.html

Related Q&A

openpyxl chage font size of title y_axis.title

I am currently struggling with changing the font of y axis title & the charts title itself.I have tried to create a font setting & applying it to the titles - with no luck what so ever. new_cha…

Combination of all possible cases of a string

I am trying to create a program to generate all possible capitalization cases of a string in python. For example, given abcedfghij, I want a program to generate: Abcdefghij ABcdef.. . . aBcdef.. . ABCD…

How to change download directory location path in Selenium using Chrome?

Im using Selenium in Python and Im trying to change the download path. But either this: prefs = {"download.default_directory": "C:\\Users\\personal\\Downloads\\exports"} options.add…

Keras, TensorFlow : TypeError: Cannot interpret feed_dict key as Tensor

I am trying to use keras fune-tuning to develop image classify applications. I deployed that application to a web server and the image classification is succeeded.However, when the application is used …

How to get matplotlib to place lines accurately?

By default, matplotlib plot can place lines very inaccurately.For example, see the placement of the left endpoint in the attached plot. Theres at least a whole pixel of air that shouldnt be there. In f…

Using Flask as pass through proxy for file upload?

Its for app engines blobstore since its upload interface generates a temporary endpoint every time. Id like to take the comlexity out of frontend, Flask would take the post request and forward it to th…

What does printing an empty line do?

I know this question may well be the silliest question youve heard today, but to me it is a big question at this stage of my programming learning.Why is the second empty line needed in this Python code…

Django - how do I _not_ dispatch a signal?

I wrote some smart generic counters and managers for my models (to avoid select count queries etc.). Therefore I got some heavy logic going on for post_save. I would like to prevent handling the signa…

Python 3 Decoding Strings

I understand that this is likely a repeat question, but Im having trouble finding a solution.In short I have a string Id like to decode:raw = "\x94my quote\x94" string = decode(raw)expected f…

how get context react using django

i need get context in react using django but i cant do itthis is my code in my jsx <h1>{{titulo}}</h1> <h2>{{ejemplo}}</h2>in my template:{% load staticfiles %} <!DOCTYPE ht…