simple python question, will probs only take u 2 secs

29 views
Skip to first unread message

George Mason

unread,
Mar 15, 2017, 9:48:27 AM3/15/17
to Cambridge and East Anglian Python Users Group
how do i say any number in python
e.g. 
A = input("what is A")
if A == "any number":
print("...")

thank you

Ben Nuttall

unread,
Mar 15, 2017, 10:00:24 AM3/15/17
to cam...@googlegroups.com
In that case, A is going to be a string, so you'll need to convert it to an integer. If, when converting it, you get an error, you know it's not a number. Python has a neat way of dealing with this:

What do you want to do with the number? Perhaps you can use that to determine whether it's a number. For example if you want to add 1 to it:

A = input("What is A?")
try:
    A = int(A)
except ValueError:
    print("A is not a number")
else:
    print(a+1)



Ben

--
You received this message because you are subscribed to the Google Groups "Cambridge and East Anglian Python Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to campug+unsubscribe@googlegroups.com.
To post to this group, send email to cam...@googlegroups.com.
Visit this group at https://groups.google.com/group/campug.
For more options, visit https://groups.google.com/d/optout.

Ben Nuttall

unread,
Mar 15, 2017, 10:00:57 AM3/15/17
to cam...@googlegroups.com
int or float - as appropriate

Ben
Reply all
Reply to author
Forward
0 new messages