I need to write a program that will check the numbers that a user enters. If the user enters a number more than once then it will skip over it and print out only numbers that the user entered once.
I was playing around with this:
def single_element():
numbers = []
numbers = input("Enter some numbers: ").split()
for i in numbers:
if i in numbers:
i + 1 #I was trying to find a way to skip over the number here.
print(numbers)
You can build a set to just print unique numbers:
numbers = input("Enter some numbers: ").split()
print set(numbers)
Use a set. They are iterables, like lists, and can easily be converted back and forth. However, sets do not contain duplicate values.
def single_element():
numbers = list(set(input("Enter some numbers: ").split()))
print(numbers)
In this function, you get the input numbers as a list and convert them to a set, which will remove duplicates, and then convert back to a list.
Note: sets are not guaranteed to keep the same order like lists.
Related
i create a program that reads a sequence of numbers, determines how many different numbers there are (we count the repetitions once), and writes the result to the standard output.
my first code:
f=int(input("String of numbers: "))
l=[]
for x in range(f):
string_numbers = int(input(f'Enter {x+1} string of numbers: '))
l.append(string_numbers)
mylist = list(dict.fromkeys(l))
print(len(mylist))
I wanted to take into account if the user entered a string too short or too long than declared. I wanted the user to type everything on one line. When I enter an incorrect string number, I get duplicated "incorrect string lengthincorrect string length"
f=int(input("String of numbers: "))
my_list = input('Enter numbers in the string, separated by spaces: ').split()
list_of_integers=[]
l=len(str(list_of_integers))
for i in my_list:
list_of_integers.append((i))
mylist = list(dict.fromkeys(list_of_integers))
for i in range(f):
if i < l:
print("incorrect string length", end='')
elif i > l:
print("incorrect string length", end='')
else:
It seems like you're mixing up your different variables -- f is what you want the length to be, l is just the number 2, and the way you're comparing those two has nothing to do with the actual input entered by the user, which is my_list.
Using variable names that indicate their meaning might make it easier to keep it all straight:
num_count = int(input("Length of string of numbers: "))
num_list = input('Enter numbers in the string, separated by spaces: ').split()
if len(num_list) == num_count:
print(f"there are {len(set(num_list))} different numbers")
else:
print("incorrect string length")
In the above code, num_count is the count of how many (non-unique) numbers you expect them to input, and num_list is the actual list. To figure out if the list is the expected length, compare num_count to len(num_list).
Note that since all you're doing is looking for unique values, converting the strings in num_list to int is not necessary (whether or not you use a set as I've done here).
You will most likely be better off using another function that ultimately has a while loop. This will make sure that when the user is giving the input that if anything is malformed you can then parse it checking and finally making sure to prompt the user again.
For example:
f=int(input("String of numbers: "))
my_list = input('Enter numbers in the string, separated by spaces: ').split()
list_of_integers=[]
l=len(str(list_of_integers))
for i in my_list:
list_of_integers.append((i))
mylist = list(dict.fromkeys(list_of_integers))
for i in range(f):
# XXX Here call your "input-function"
get_user_input(i, l)
def get_user_input(user_len, len):
while True user_len != len:
print('Incorrect Input')
user_len = int(input("String of numbers: "))
return
This is not exactly a working example but with what you have you get the idea that you want to do a while loop until your inputs match.
numbers = list (input("Please enter your numbers: "))
print (numbers)
This is all I have when I start the code it only shows single digit numbers but when I put 12, I get '1' '2' how do I make it stay 12 and sort the numbers smallest to biggest?
input("Please enter your numbers: ") - this returns string
list(string) - will separate string on every character and make list of them
I think this will do what you want:
numbers = [int(x) for x in input("Please enter your numbers: ").split(' ')]
print (numbers)
I used split() function, and when i say string.split(' ') it will separate string on every ' ' and return the list.
For sorting the list you can use the built-in function sort(), or you can make your own. This page will help you to make your sorting function with selection sort.
I'm having a problem of printing a string inside an input. I made a for loop for automatic numbering based on how many elements that the user wants to input.
list = []
n = int(input("How many elements you want to input: "))
for i in range(0, n):
element = int(input(i+1))
if n == element:
print("hello")
break
list.append(element)
For example, I inputted 3 in the number of elements. I want to make my program output be like this:
input
input
input
(input is the user will type once the number is shown)
But my program looks like:
1input
2input
3input
I just want to work up with the design, but I don't know how to do it.
What you need is called string formatting, and you might use .format by replacing
element = int(input(i+1))
using
element = int(input("{}. ".format(i+1)))
or using so-called f-strings (this requires Python 3.6 or newer):
element = int(input(f"{i+1}. "))
If you want to know more, I suggest reading realpython's guide.
Try:
input(str(i+1)+'. ')
This should append a point and a space to your Text. It converts the number of the input to a String, at which you can append another String, e.g. '. '.
You have to edit the input in the loop to something like this:
element = int(input(str(i+1) + ". "))
You are close. Convert i+1 to a string and concatenate a . to it and accept input.
Note: Do not use list as a variable name. It is a Python reserved word.
lst = []
n = int(input("How many elements you want to input: \n"))
for i in range(n):
element = int((input(str(i+1) + '. ')))
if n == element:
print("hello")
break
lst.append(element)
How many elements you want to input:
5
1. 1
2. 6
3. 7
4. 4
5. 6
n = int(input("How many elements you want to input: "))
for i in range(0, n):
print(str(i+1) + ". " + str(n))
This should do.
I used the same code shape as yours, and that way it is easier for you to understand.
I am trying to make an average calculator so that you can input as many numbers as you like and I am trying to turn an input into a list, how can I do this? Here is my code that I have so far.
numbers = []
divisor = 0
total = 0
adtonum = int(input("Enter numbers, seperated by commas (,): "))
numbers.append(adtonum)
for num in numbers:
divisor += 1
total = total + num
print(num)
print("Average: ")
print(total / divisor)
Try this:
adtonum = input("Enter numbers, separated by commas (,): ")
numbers = [int(n) for n in adtonum.split(',')]
Here, we split the line up by the delimiter (in this case a comma) and use list comprehension to construct the list of numbers -- converting each of the numbers in the input string into integers one by one.
Try this code.
# assign values using unpacking
divisor, total = 0, 0
# list comprehension
numbers = [int(x) for x in input("Enter numbers, separated by commas (,): ").split(',')]
for num in numbers:
divisor += 1
total += num
print(num)
print("Average: ")
print(total / divisor)
You can use eval function:
numbers = eval(input("Enter a list of numbers i.e; values separated by commas inside '[]' "))
Here you go;
divisor = 0
total = 0
adtonum = (input("Enter numbers, seperated by commas (,): "))
numbers = adtonum.split(',')
for num in numbers:
divisor += 1
total = total + int(num)
print(int(num))
print("Average: ")
print(total / divisor)
Explanation:
As you were trying to get the input from the user but the input provided by the user couldn't be parsed into int because it contained ,.
I simply got the input from the user, splited the input at commas. Note that split function returns a list of elements seperated by the character provided as the argument. Then i iterated over this list, parsed every element as int which is possible now. Rest is same.
You could use the eval() function here, like this-
numbers = list(eval(input("Enter numbers, seperated by commas (,): ")))
Since the input is just comma-separated numbers thus, the eval() function will evaluate it as a tuple, and then the list() function will convert it into a list.
I'm still learning Python and I ran into a problem. My professor wants me to ask the user for input of a number with several digits with nothing separating them. Then, he wants me to write a program that would add those digits together and print the result. I can't do it because I don't understand how.
This is what I'm trying:
inp = input("Please enter a number with several digits with nothing separating them: ")
for number in inp:
count += int(len[inp])
print(count)
There are other ways I tried to do this, but it's just not working. What am I doing wrong? Exactly how should I do this? This is from Chapter 6 in "Python for Everybody" book.
First of all you need to define count variable:
count = 0
input() method returns a string without the trailing newline. You can iterate over characters in inp to sum up their numeric values:
for n in inp:
count += int(n)
Did you try in that way?
inp = input("Please enter a number with several digits with nothing separating them: ")
count=0
for number in inp:
count += int(number)
print(count)
Example if the user enter 25, the result should be 7, is it?
you need to add count variable to store the sum of each iterable value
instead of count+=int(list(inp)) you need to use count+=number as you are iterating the input string and already accessing each digit in for loop
inp = input("Please enter a number with several digits with nothing separating them: ")
count = 0
for number in inp:
count += int(number)
print(count)