How to add input how many integers do you want? - python

I'm starting next month full stack developer and im doing some practicing
i started with Python and i want to make some code
with while loop that will ask the user to input how many integers they want
and i want to calculate all the numbers
im doing something wrong not sure what
thanks in advance
oz
example:
number = int(input('Enter how many integer: '))
my_list = [number]
while len(my_list) < number:
user_input = int(input('Enter a integer: '))
my_list.append(user_input)
print(user_input+number)
print(my_list)

number = int(input('Enter how many integer: '))
my_list = []
while len(my_list) < number:
user_input = int(input('Enter a integer: '))
my_list.append(user_input)
print(user_input, ' ' ,number)
print(my_list)

Related

Finding items in lists using the in operator

I created some code where I try to find elements in a list given by the user using the in operator.
productNums = []
nums = int(input("How many numbers should be in your list?"))
while nums > 0:
productNums.append(input("Add a number: "))
nums -= 1
numFind = int(input("What number are you trying to find? "))
if numFind in productNums:
print("This product number is in the list")
elif numFind not in productNums:
print("Ahh, sorry. This product number is NOT in this list!")
else:
print("Huh, you're confusing me now, check your numbers pal!")
When I run this, I get the outcome that the number is not in this list, even though it is in it. Can someone tell me what I am doing wrong?
As people said above, just small issue with you integers and strings.
This is your code working:
Be careful with the indentention, initially was a mess.
productNums = []
nums = int(input("How many numbers should be in your list?"))
while nums > 0:
productNums.append(int(input("Add a number: ")))
nums -= 1
numFind = int(input("What number are you trying to find? "))
if numFind in productNums:
print("This product number is in the list")
elif numFind not in productNums:
print("Ahh, sorry. This product number is NOT in this list!")
else:
print("Huh, you're confusing me now, check your numbers pal!")
I believe its a missing conversion when u append, its appended as a string and u search for an int
Because the productNums list contains string values, not integer, so you always compare int with str variable and obtain the same result. Set productNums elements to int and it will work properly.
productNums = []
nums = int(input("How many numbers should be in your list?"))
while nums > 0:
productNums.append(int(input("Add a number: ")))
nums -= 1
numFind = int(input("What number are you trying to find? "))
if numFind in productNums:
print("This product number is in the list")
elif numFind not in productNums:
print("Ahh, sorry. This product number is NOT in this list!")
else:
print("Huh, you're confusing me now, check your numbers pal!")
You correctly cast nums and numFind to int, but you didn't do so for the input values in the while loop — so productNums is a list of single-character strings.
This change will fix it:
productNums.append(int(input("Add a number: ")))
The problem is that you are trying to find an int in a list of strings.
When you wrote the code productNums.append(input("Add a number: ")), after the user enters the code, the number would be appended into the productNums list as a string.
Further on, the numFind = int(input("What number are you trying to find? ")) makes the user enter a number. this time, the input is converted into an int. So when the line of code if numFind in productNums: runs, it sees the if as the user trying to find an int in a list of strings.
In short, you should either change the productNums.append(input("Add a number: ")) code into productNums.append(int(input("Add a number: "))), or change the line of code numFind = int(input("What number are you trying to find? ")) into numFind = input("What number are you trying to find? ").

how to print sum of numbers between any given numbers using loop python

how can i print the sum of all the numbers between any two given numbers in python.
i am not allowed to use functions like sum(). i can only use while or for loop.
i have a code which does the job but the problem is it also prints result every time after adding two numbers but i only want it to print final sum of all the numbers.
Here's the code:
...
a = int(input("Please enter a number"))
b = int(input("Please enter a number"))
n = 0
for x in range(a+1,b):
n+=x
print(n)
...
thanks
There is an Indentation Error just check the line below the for loop and give the proper indentation. You can refer to my code
a = int(input("Please enter a number: "))
b = int(input("Please enter a number: "))
n = 0
for x in range(a+1,b):
n+=x
print(n)

printing smallest integer from user input

I have seen things on how to sort out a list but I am having trouble figuring out how print the smallest number that a user inputs from from a total of 3 numbers that they input. I have found some answers on here for other languages and I am sure this has been asked before but I have had trouble finding anything that helps this particular assignment.
user_input = int(input())
smallest_number =
print(smallest_number)
a = int(input('Enter number: '))
b = int(input('Enter number: '))
c = int(input('Enter number: '))
lst = [a,b,c]
smallest_number = min(lst)
print(smallest_number))
Or a shorter way would be
smallest_number = min([int(input('Enter Number: ')) for count in range(3)])
print(smallest_number)
min just returns the smallest number
Input:
Enter Number: 4
Enter Number: 5
Enter Number: 7
Output
4
Found some help elsewhere but let me post what we came up with for other people to read later.
print('enter 3 integers')
user_input_1 = int(input())
user_input_2 = int(input())
user_input_3 = int(input())
inputs = [user_input_1, user_input_2, user_input_3]
smallest_number = min(inputs)
print(smallest_number)
This solution works for any number of inputs.
list = map(int, input().split())
print(min(list))
Input: 4 2 5
Output: 2
Input: 9 7 5 3 4
Output: 3

Beginner: Creating a list with divisors

I just started learning Python and am stuck with an exercise.
The program should ask the user for a number and then print out a list including all of the number's divisors.
myList = []
usernumber = int(input("Please enter a number: "))
a = int(1)
for a in range(1, usernumber):
while usernumber % a == 0:
divisor = usernumber / a
myList.append(divisor)
a += 1
print(*myList)
This seems to work for everything except 1, but I can't figure out what I have to change to make it work for an input of 1. Any ideas?
Try this:
myList = []
usernumber = int(input("Please enter a number: "))
#No need to declare a as an integer, for loop does that for you.
for a in range(1, usernumber+1):
#usernumber+1 as range() does not include upper bound.
#For each number leading up to the inputted number, if the
#remainder of division is 0, then add to myList.
if usernumber % a == 0:
myList.append(a)
print(myList)

How to count how many tries are used in a loop

For a python assignment I need to ask users to input numbers until they enter a negative number. So far I have:
print("Enter a negative number to end.")
number = input("Enter a number: ")
number = int(number)
import math
while number >= 0:
numberagain = input("Enter a number: ")
numberagain = int(numberagain)
while numberagain < 0:
break
how do I add up the number of times the user entered a value
i = 0
while True:
i += 1
n = input('Enter a number: ')
if n[1:].isdigit() and n[0] == '-':
break
print(i)
The str.isdigit() function is very useful for checking if an input is a number. This can prevent errors occurring from attempting to convert, say 'foo' into an int.
import itertools
print('Enter a negative number to end.')
for i in itertools.count():
text = input('Enter a number: ')
try:
n = int(text)
except ValueError:
continue
if n < 0:
print('Negative number {} entered after {} previous attempts'.format(n, i))
break
The solution above should be robust to weird inputs such as trailing whitespace and non-numeric stuff.
Here's a quick demo:
wim#wim-desktop:~$ python /tmp/spam.py
Enter a negative number to end.
Enter a number: 1
Enter a number: 2
Enter a number: foo123
Enter a number: i am a potato
Enter a number: -7
Negative number -7 entered after 4 previous attempts
wim#wim-desktop:~$

Categories

Resources