Print distinct numbers in python 3 [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following code to print the (distinct numbers), but I'm trying to check the inputs before the process, should be ten digits numbers only with only one space.
I tried while and try but still can't figure out:
def main():
list1 = input("Enter ten numbers: ").split()
set1 = set(list1)
print(set1)
list2 = list(set1)
string = string.join(list2)
print("The distinct numbers are: " + str(string))
main()

A simpler version using the isnumeric() method built in strings. Also, I put a loop around the input so as the user can easily retry in case of wrong data:
def main():
while(True):
numbers = input("Enter ten space-separated numbers: ").split()
if len(numbers) != 10 or not all(n.isnumeric() for n in numbers):
print("Wrong input, please retry")
continue
numbers = ' '.join(set(numbers))
print("The distinct numbers are:", numbers)
break # or return numbers if you need the data
main()

Do you mean check if there are ten numbers inputted?
import re
def check_input(input_string):
compare_to = '\s'.join(10*['\d+'])
if re.match(compare_to[:-1], input_string):
return True
return False
This is a regular expression, it checks if the input string is equal to a specified input format. This one specifically checks whether 10 sets of at least 1 number [\d+] are added with a space inbetween \s.

Related

Would anyone know the code in python that prints ONLY if it's a binary string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
The real problem is there:
write a program that takes a string as input and prints “Binary Number” if the string contains only 0s or 1s. Otherwise, print “Not a Binary Number”.
You can iterate every character of the input and verify it is a zero or a one. The all function can be used for this iteration and check.
Also require that the string has at least one character:
def isBinary(string):
return all(ch in "01" for ch in string) and string != ''
Example use:
string = input("Enter a binary number: ")
if isBinary(string):
print("Thank you!")
else:
print("That is not a binary number.")
It is easy.
def is_binary(string_to_check:str):
for x in string_to_check: #loop though all letter
if x in ('0','1'):
continue #yep the letter is binary thingy
else:
return False #no
return True #yep the string is binary
Next time check google before ask.
def check_if_binary(string) :
p = set(string)
s = {'0', '1'}
if s == p or p == {'0'} or p == {'1'}:
print("Binary Number")
else :
print("Not a Binary Number")
string = str(input ("Enter the sting : "))
check_if_binary(string)
References: https://www.geeksforgeeks.org/python-check-if-a-given-string-is-binary-string-or-not/

How can I print digits at even indexes of a number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Let's say x is an integer number. How can I print every even digit of this number?
For example, if the input is 34567910, then I want the output to be 4 6 9 0.
I know how to print every single digit using the following code, but I can't figure out how to print even digits only:
for i in str(x):
print(i)
Here is the solution to your problem. Note that I have to check if i is odd, due to Python indexes starting from 0. For example, in Python, an index of 1 is the second position.
num = input("Enter a number: ")
return_num = ""
# Iterates through input
for i in range(len(num)):
# Checks if digit is at even position
if i % 2 == 1:
# If so, adds it to return_num
return_num += num[i] # + " " if you want spaces between numbers
print(return_num) # Prints 4690 with your input
Alternatively, you could achieve this using one for-loop. (Credit to OneCricketeer.)
num = input("Enter a number: ")
return_num = ""
# Iterates through specified indexes of input
for i in range(1, len(num), 2):
return_num += num[i] # + " " if you want spaces between numbers
print(return_num) # Prints 4690 with your input
Or, if you want to have the shortest program humanly possible to solve your problem (Credit to Tomerikoo):
num = input("Enter a number: ")
print(num[1::2]) # Prints 4690 with your input
This is one way how you can do it
# iterate over string
x = 34567910
string = str(x)
for index in range(len(string)):
# check if index is divisible by 2
if index % 2 != 0:
# print character at index
print(string[index], end = '')
x = str(input())
Try using slicing and map method:
list(map(int, list(x)[1::2]))
[4, 6, 9, 0]

Program that prompts the user for a non-negative integer n, and then writes a sum of the even digits of n [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want a program that sums the even numbers of a bigger number using while function.
Exemple :
Number is : 12345
Print : 6 (2+4)
This is what i wrote so far:
num = int(input("Introduce a non negative number: "))
if num % 2 == 0:
sum += num
print("sum")
I can't stress this enough
When doing school assignments, the whole idea with assignments is to teach you concepts, not solutions. There's a reason why you were given this assignment, asking others to solve it for you - is not the way to go.
Go back to your teacher, and ask for help if something is unclear. But because others start posting solutions I might as well just keep mine here.
Skipping the conversion to an early integer, will allow you to iterate over it as a string, and grab one number a a time.
num = input("Introduce a non negative number: ")
total = 0
for i in num:
if int(i) % 2 == 0:
total += int(i)
print("sum:", total)
You can then use your original logic, with some minor modifications.
Since for whatever reason, you're only allowed to use while and not for, you'd have to just adapt a bit.
num = input("Introduce a non negative number: ")
total = 0
i = 0
while i < len(num):
if int(num[i]) % 2 == 0:
total += int(num[i])
i += 1
print("sum:", total)
While I'm at it, after reading my code again. I am quite sure that a while loop here is the least pretty solution to this problem. But from a teaching standpoint there might be some benefit here. But I'd recommend going with the for loop if at all possible.
Try this one:
# split the number to list of digits
digits_list = list(input("Introduce a non negative number: "))
total = 0
# loop over the digits list and pick a digit till the list is empty
while digits_list:
digit = digits_list.pop()
if int(digit) % 2 == 0:
total += int(digit)
print(total)

How to create different output with one input statement in a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am wondering how to create different output with one input statement in a for loop (see my code below). For instance, how should I make each input statement read Enter number 1:, Enter number 2: and so on. Should I have only one input statement or multiple?
times = int(input("Enter how many numbers you want to sum"))
sum = 0
for i in range(0, times):
numInput = int(input("Enter number"))
sum = sum + numInput
print("The total is", sum)
One input is enough because you can change the string in your input prompt like this:
times = int(input("Enter how many numbers you want to sum? "))
sum = 0
for i in range(1, times+1):
numInput = int(input("Enter number {}:".format(i)))
# or use below code:
# numInput = int(input("Enter number %d:" % (i)))
sum = sum + numInput
print("The total is", sum)
To learn more about formatting python strings please refer to this link:
https://pyformat.info/

Python 3- password check [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the problem: write a function that checks whether a string is valid password.
Rules: Must have at least 8 characters A password must consist of only letters and digits A password must contain at least 2 digits
I am having trouble with def CountsDigitsFor, I want it to check for at least 2 digits.
def getPassword():
return input("Enter password: ")
def CountDigitsFor(password):
res = []
for i in password:
if i.isdigit():
res.append(i)
return int
def validPassword(password):
if len(password) >= 8:
if password.isalnum():
if CountDigitsFor(password) >= 2:
return True
else:
return False
def main():
password = getPassword()
if validPassword(password):
print(password + " is valid")
else:
print(password + " is invalid")
main()
return int
is just returning the class int. What you want to return is the number of digits. That of course would be:
return len(res)
Why not just use this one:
def CountDigitsFor(password):
return sum(character.isdigit() for character in password)
I have provided the above in your other question, but just in case that wasn't clear I am going to give you a brief description on how this works.
The above is equivalent to what you want but in one single line. The idea is that you loop through the characters of the password, check if the character is a number and then get the sum of the digits in the password.
Then after you call the CountDigitsFor() you will get the number of digits in the password and you can check if they are at least 2 with the if statement you have provided.
if CountDigitsFor(password) >= 2:
return True

Categories