So what i want is for my code to understand if my input has a number in it or not, if it does it is meant to output "Correct" but if it doesn't then it would output "Incorrect" how can i actually make this work. So it knows if there is an integer in the input or not. Any help is appreciated
import re
yourString=input()
number = re.search(r'\d+', yourString).group()
if number == True:
print("Correct")
else:
print("Incorrect")
According to https://docs.python.org/3/library/re.html#match-objects you have a bit of a subtle error.
The statement should not be if number == True: it should be if number:, the reason being the value number is tests as True if anything is matched, and is None if there are no matches.
i.e.
import re
yourString=input()
number = re.search(r'\d+', yourString)
if number:
print("Correct")
group = number.group()
else:
print("Incorrect")
Note, somewhat bizarrely
import re
yourString=input()
number = re.search(r'\d+', yourString)
if number:
print(number == True)
group = number.group()
else:
print("Incorrect")
Prints false, so number is not the value true, it overrides __bool__. See https://docs.python.org/3/reference/datamodel.html#object.bool for more information
You can use str.isdigit, re.search, or the built-in string to number conversions such as float and int:
def extractNumber(string):
for word in string.split():
try:
# The float() function accepts a decimal string.
# The int() function accepts an integer string.
return float(word)
except ValueError:
pass
number = extractNumber(input())
if number is not None:
print('Your number is: {}'.format(number))
else:
print('No number found')
the regex you're looking for is: \w*[0-9]+\w*. It accepts anything that contains at least one digit.
\w* is the same as [a-zA-Z0-9_]* meaning it will match any lower/upper -case letter, digit or _ literally. It can occur 0 to many times in the string
[0-9]+ matches one to many digits from 0 to 9.
Actually, if your intention is just to check whether the string has digits you are good to go just with [0-9]+ that's the same of what you posted originally.
You can try and create regexes using this site regex101.com
Related
I can accept user's input in two formats:
123
123,234,6028
I need to write a function/a few lines of code that check whether the input follows the correct format.
The rules:
If a single number input, then it should be just a number between 1 and 5 digits long
If more then one number, then it should be comma-separated values, but without spaces and letters and no comma/period/letter allowed after the last number.
The function just checks if formatting correct, else it prints Incorrect formatting, try entering again.
I would assume I would need to use re module.
Thanks in advance
You can use a simple regex:
import re
validate = re.compile('\d{1,5}(?:,\d{1,5})*')
validate.fullmatch('123') # valid
validate.fullmatch('123,456,789') # valid
validate.fullmatch('1234567') # invalid
Use in a test:
if validate.fullmatch(your_string):
# do stuff
else:
print('Incorrect formatting, try entering again')
Another option is:
def validString(string):
listofints = [v.isdigit() for v in string.split(",") if 0 < len(v) < 6]
if not len(listofints) or not all(listofints):
print("Incorrect formatting, try entering again.")
The following code should leave the Boolean variable valid as True if the entered ibnput complies with your rules and False otherwise:
n = input("Your input: ")
valid = True
try:
if int(n) > 9999:
valid = False
except:
for character in n:
if not character in "0123456789,":
valid = False
if n[-1] == ",":
valid = False
I'm trying to make sure that every word that is typed is uppercased as in ABC. The code I'm working with is this
abbreviate=input("Abbreviation: ")
words=abbreviate[0:6]
numbers = abbreviate[6:8]
if len(abbreviate)==9:
if words.isalpha: #I think it's also possible to use.if str(words)?
if words.isupper: #(i did upper first, what is the difference?)
if int(numbers):
print("correct")
else:
print("wrong, you need numbers")
else:
print("wrong, all words are supposed to be uppercase")
else:
print("wrong, it needs to be words(alphabet)")
else:
print("wrong, the length needs to be 8")
QWERTY567 should be correct.
qwerty567 should be incorrect.
How do I go on doing this?
qwerty333
012345678
IIUC In order to just verify, whether first 5 characters are uppercase letters, and next 4 are digits, you can do:
import re
abbreviate=input("Abbreviation: ")
if(re.match(r"^[A-Z]{5}\d{4}", abbreviate)):
print("correct!")
else:
print("wrong!")
If moreover you want to ensure it's only 9 characters in total (i.e. input consists of exactly 5 uppercase letters first, then 4 digits) you can do:
import re
abbreviate=input("Abbreviation: ")
if(re.match(r"^[A-Z]{5}\d{4}$", abbreviate)):
print("correct!")
else:
print("wrong!")
str.isalpha and str.isupper are methods. You need to call them to get a result:
if words.isalpha():
if words.isupper():
About your comments
I think it's also possible to use if str(words)?
No, str(words) would do nothing. input() returns a string, so words is already a string.
I did upper first, what is the difference?
str.upper converts a string to uppercase, e.g. 'a'.upper() == 'A'
By the way
int() doesn't return a Boolean. It might be better to use str.isnumeric to check if numbers is numeric instead.
It's simpler to use guard clauses than nested conditionals:
if len(abbreviate) != 9:
print("wrong, the length needs to be 9")
elif not words.isalpha():
print("wrong, the abbreviation must be alphabetic")
...
else:
print("correct")
Use a regular expression:
^[A-Z0-9]+$
See a demo on regex101.com.
If you have all letters and then at a certain index, you have all numbers, I would suggest:
abbreviation=input("Abbreviation: ")
i = len(abbreviation)
for n, char in enumerate(abbreviation):
if char.isalpha() and char.isupper():
pass
else:
i = n
break
for char in abbreviation[i:]:
if char.isnumeric():
pass
else:
print('Wrong')
https://www.geeksforgeeks.org/python-string-isnumeric-application/
https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/
You find the first non-lowercase letter, and then everything after it should be all numeric. If you need at least 1 uppercase letter, you can also modify that. Please tell me if you have questions!
If the value of abbreviate is 'QWERTY567', then you should edit your code with words = abbreviate[0:6] , numbers = abbreviate[6:8] , isalpha() and isupper() for it to work.
I am a new Python learner. I wrote the code below which is for a program that reads a number (eg. 1234), adds up all the digits (the result would be 10), and displays the result.
It works if I remove the regular expression part. However, I would like to know why the code does not work with it?
import re
numbers= int(input("Enter a number e.g. 1234 :"))
n=re.match('(\d+)', number)
if n:
total = 0
for number in numbers:
total += number
print ("The total sum is:",total)
else:
print ("invalid input!")
Thank you all!
re.match returns a match instance:
>>> import re
>>> number='123' # just an example
>>> re.match('(\d+)', number)
<_sre.SRE_Match object; span=(0, 3), match='123'>
What you need is what was matched:
>>> re.match('(\d+)', number).group()
'123'
An example of why the regex might be useful is the case of removing unwanted strings. For example:
>>> number='123 kg'
>>> re.match('(\d+)', number).group()
'123'
To sum over digits:
>>> number='123 kg'
>>> digits = re.match('(\d+)', number).group()
>>> sum(int(c) for c in digits)
6
Regex is for matching and comparing against strings. When you convert the input to int regex will fail because it is no longer comparing to a string.
Also, you cannot iterate over an integer. I think what you want to do is to check if the string input is all digits, then iterate over the digits in the string, convert them to ints, and sum as you go.
try this:
import re
number= input("Enter a number of variable length e.g. 73618 :")
n=re.match('(\d+)', number)
if n:
total = 0
for digit in number:
total += int(digit)
print ("The total sum of digits is:",total)
else:
print ("Error! Make sure you only use numbers")
As per python docs :
re.match(pattern, string, flags=0)
If zero or more characters at the beginning of string match the
regular expression pattern, return a corresponding MatchObject
instance. Return None if the string does not match the pattern
So what you need to do here is check if the returned value from regex is not None.
ie, if n is not None:
The other answers got pieces right, but it really is much simpler than what you're making it anyway.
You're getting the error because re.match works with strings, not integers, so you need:
number = input("Enter a number of variable length e.g. 73618 :")
Also, if re does not match, it will return None. None is not the same as False:
if n is not None: ...
It also doesn't work if you mix numbers and letters (e.g. 9f). The solution is very simple though. You don't need to use re at all:
number = input("Enter a number of variable length e.g. 73618 :")
if number.isdigit():
total = 0
for digit in number:
total += digit
print ("The total sum of digits is:",total)
else:
print("Error! Make sure you only use numbers")
If you are only trying to enter a single number, a regular expression might be a bit overkill here. When you use Python's int() function to convert a string into a number, it will give you an error if the string contains letters. You can catch this using exception handling as follows:
try:
number_string = input("Enter a number of variable length e.g. 73618 : ")
number = int(number_string) # try and convert the string into a number
total = 0
for digit in number_string:
total += int(digit)
print ("The total sum of digits is:", total)
except ValueError as e:
print ("Error! Make sure you only use numbers")
You could also make use of Python's sum() function to help with the totalling:
try:
number_string = input("Enter a number of variable length e.g. 73618 : ")
number = int(number_string) # try and convert the string into a number
total = sum(int(digit) for digit in number_string)
print ("The total sum of digits is:", total)
except ValueError as e:
print ("Error! Make sure you only use numbers")
I need some advice for this challenge. The requirements are to see if the input is valid (i.e. only 0s and 1s, no spaces, and no letters) and accepted (contains two 1s). Then, if the input is valid and accepted, translate the binary into decimal. Any help would be appreciated!
#Examples of invalid binary numbers: abc 10102011 10101FF
#0000 1111 (note:contains a space)
#Examples of valid, rejected binary numbers: 00000000 1111 01110000001
#Examples of valid, accepted binary numbers: 1000001 11000000 1111
binary = str(input("Enter a binary number: "))
binary_list = list(binary)
valid = True
accepted = True
convert = ""
var = binary_list.count('1')
for character in binary_list:
if (character != '1') and (character != '0'):
valid = False
for character in binary_list:
if (var != 2):
accepted = False
if (valid == True and accepted == True):
print("Input", binary ,"is valid and accepted")
convert = int(binary, 2)
print ("The number is ", convert)
elif (valid == False):
print ("Input was invalid")
elif (valid == True and accepted == False):
print ("Input was rejected")
You can use sets to check if the input only contains 0's and 1's.
>>> set("1101101000100001001010101")
{'0', '1'}
No matter what the result is, it should only contain some subset of {'0', '1'}. (There's a chance it won't have one or the other.) We can use the set.issubset() method to check this. (Note the 2 in the second example.)
>>> set("11010001").issubset(('0', '1'))
True
>>> set("11010201").issubset(('0', '1'))
False
Finally, like you found, you can use str.count() to figure out if there are exactly two 1's.
>>> "1001".count('1')
2
Here's the entire code block.
if not set(binary).issubset(('0', '1')):
print("Input was invalid binary.")
elif binary.count('1') != 2:
print("Input was rejected.")
else:
print("Input", binary, "was valid and accepted!")
Note the reordering of the statements; rather than first checking for entire validity and otherwise trying to determine points of failure, we can check for individual fail cases with if/elifs, and then place the success case afterwards in the else block.
Edit: If you would like to keep using the strategy that you outlined in your code example, you can add break in the if block of the first for loop, to stop searching after a fail case was found. The second for loop is also unnecessary, as you are not using the character at all, and it can be removed (keeping the if block "outside").
Also, note that the conversion from binary to binary_list is unnecessary, as strings may be iterated over and have the .count() method, as well.
for character in binary:
if character != '1' and character != '0':
valid = False
break
if var != 2:
accepted = False
import re
def check_input():
while True:
try:
sequence = raw_input("Please input:")
if sequence = [a,t,c,g]: # checking for valid input
continue
else:
print("invalid input, sequence coule only contain the "
"following letters (a,t,c,g)"):
check_input()
I basically want the script to check the user's input whether it contains only these four letters (a,t,c,g). If their input contains anything other than that four letters, it could print that second statement and prompt the user to input again. I saw there are similar questions and I already tried to change my script according to those posts but it still gives me the invalid syntax error at the if < sequence position. Anyone knows what's wrong here?
You need to iterate over every letter in the input and check if it is in the set of allowed letters, like this:
sequence = raw_input("Please input:")
for letter in sequence:
if letter not in "atcg":
print("invalid input, sequence coule only contain the following letters (a,t,c,g)")
When you discover that the sequence is invalid, you could choose to end the check by using a break statement after the print, or you could count how many invalid letters are allowed.
Your function must check and give user whether True or False:
def check_input(word):
result = True
for letter in sequene:
if letter in 'atcg':
continue
else:
result = False
break
return result
check_input('atgc')
Error Message:
if check_input('agct'):
continue
else:
print "error message"
You could also use the filter command:
def checkInp():
seq = raw_input("Please input sequence:")
if not filter(lambda m: m not in 'ATGC', seq) == '':
print 'Invalid input characters in sequence: ' + filter(lambda m: m not in 'ATGC', seq)
print 'Pleas'
check_input()
else: return seq
sequence, once input by the user will be a string, so you would need to iterate over each character in the sequence and use in to verify the existence of the character in the accepted characters string. String comparisons in Python are also case sensitive, so you need to match the case of the input to your expected string. I've used uppercase based on your sample input.
def check_input():
sequence = input("Please input:")
sequence.upper()
for letter in sequence:
if letter in 'ATCG':
continue
else:
print("invalid input, sequence could only contain the
following letters: a, t, c or g.")