how to display matching digits in a string from another number [duplicate] - python

This question already has answers here:
How to compare string and integer in python? [duplicate]
(2 answers)
Closed 2 years ago.
I'm newbie to python. I'm trying to find matching digits in a string from a given number. I have tried the following code:
st=input("Enter a string")
no=int(input("Enter a number"))
for i in range(len(st)):
while no>0:
rem=no%10
if rem == st[i]:
print(rem)

Here's a code snippet for you:
# Number input
mynumber = input("Enter a number: ")
# Check if input is an integer
try:
int(mynumber)
except ValueError:
print("Sorry, not a number")
# String input
mystring = input("Enter a string: ")
# Look for matching numbers in the string and display them
match = ''
for n in mynumber:
for s in mystring:
if n == s and not n in match:
match += n
print('Matching:', match if match else 'None')

Related

How to limit input integer in single digit? [python] [duplicate]

This question already has answers here:
Python excepting input only if in range
(3 answers)
Closed 2 years ago.
what's the easiest way to limit this code integer input to a single digit?
num = ["First","Second","Third"]
num_list = []
for a in num:
x = int(input(f"Enter Your {a} number: "))
num_list.append(x)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(num_list[i],num_list[j],num_list[k])
just a quick fix
def get_single_digit_input():
while True:
a = input('please enter number')
if not a.isnumeric():
print('please enter single digit Numeric only')
continue
a = int(a)
# your logic is here
if a<=9 and a>=0:
return a
else:
print('please enter single digit Numeric only')
a = get_single_digit_input()
You can check using a while statement. If the input is not a single digit, force it back to the input statement. Also you want to check if the input is a digit and not an alphabet or special character.
Here's a way to do it: I am using the walrus operator in python 3.9
while len(x:=input('Enter a single digit :')) > 1 or (not x.isnumeric()):
print ('Enter only a single digit')
print (x)
If you are using python < 3.9, then use this.
x = ''
while True:
x = input ('Enter a single digit :')
if (len(x) == 1) and (x.isnumeric()): break
print ('Enter only a single digit')
print (x)

Python: Create an infinite list that takes user input in [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
Hey I'm trying to create an infinite list in python that takes user input in till 0 is entered, I don't really know if this is the right way to do so, here's what I did:
`n = input("Enter a list element separated by space ")
while n == 0:
break
else:
list = n.split()
print(list)`
Thank you!
This code will do what you originally described:
Numbers = []
while True:
number = int(input("Please enter a number:"))
if number == 0:
break
Numbers.append(number)
print(Numbers)
try this
l = []
while 1:
n = input("Enter a list element seperated by a space")
for x in n.split():
if x == 0:
break
l.append(int(x))

How to add the numbers from a user's input [duplicate]

This question already has answers here:
Sum the digits of a number
(11 answers)
Closed 3 years ago.
So I am trying to get a user's input and add it's numbers together.
input('Enter number: ')
And then say the user entered 458 then I wanted to add all it's digits up? I have tried using + but it doesn't work. Any solutions?
The problem you probably came across is that you have a string.
>>> answer = input('Enter number: ')
Enter number: 458
>>> print(type(answer))
<class 'str'>
But if you want to add each of the digits up, you'll need to convert each digit to an int. Thankfully, because we have a string here, we can actually iterate through each character in the string (try not to read it as "each digit in the number", because we have a string '458' here with characters '4', '5', '8'.)
>>> total = 0
>>> for character in answer:
... total = total + int(character)
...
>>> print(total)
17
Notice how I convert each character in the string to an integer, then add it to a total integer.
Perhaps this will work:
user_number = input('please enter your number')
sum = 0
for num in user_number:
sum += int(num)
print(sum)
You could convert your input into a list then use list comprehension:
num = input('Enter number: ')
sum([int(i) for i in num])

Getting two inputs in one line, and how to let the code run even if the user gives only one input? [duplicate]

This question already has answers here:
Two values from one input in python? [duplicate]
(19 answers)
Closed 2 years ago.
I want to use one line to get multiple inputs, and if the user only gives one input, the algorithm would decide whether the input is a negative number. If it is, then the algorithm stops. Otherwise, the algorithm loops to get a correct input.
My code:
integer, string = input("Enter an integer and a word: ")
When I try the code, Python returns
ValueError: not enough values to unpack (expected 2, got 1)
I tried "try" and "except", but I couldn't get the "integer" input. How can I fix that?
In order to get two inputs at a time, you can use split(). Just like the following example :
x = 0
while int(x)>= 0 :
try :
x, y = input("Enter a two value: ").split()
except ValueError:
print("You missed one")
print("This is x : ", x)
print("This is y : ", y)
I believe the implementation below does what you want.
The program exits only if the user provides a single input which is a negative number of if the user provides one integer followed by a non-numeric string.
userinput = []
while True:
userinput = input("Enter an integer and a word: ").split()
# exit the program if the only input is a negative number
if len(userinput) == 1:
try:
if int(userinput[0]) < 0:
break
except:
pass
# exit the program if a correct input was provided (integer followed by a non-numeric string)
elif len(userinput) == 2:
if userinput[0].isnumeric() and (not userinput[1].isnumeric()):
break
Demo: https://repl.it/#glhr/55341028

Python check if input number is string [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
When I run the below code, it always returns Number please no matter what I enter. Why does this happen?
number = input("please type number: ")
if type(number) != int:
print('Number please')
else:
print('number')
Try this, this should work:
string = input()
if string.isalpha():
print("It is a string. You are correct!")
elif string.isdigit():
print("Please enter a string.")
else:
print("Please enter a proper string.")
The .isalpha is used for checking if it's a string. The .isdigit is used for checking if it's a number.
Hope it helps :)

Categories