Counting with placeholders - python

How to manage that placeholder will start counting from 1. For example, if the input is 3, how can I display "enter the number 1", "enter the number 2" and "enter the number3"
numbers = int(input("how many numbers do you want? "))
for i in range(numbers):
x = int(input("enter the number " + "%d" % ()))

For this simple case, forego the placeholder and just append the string
numbers = int(input("how many numbers do you want? "))
for i in range(numbers):
x = int(input("enter the number " + str(i+1)))
Otherwise, you can look at using f-strings

I'm not old enough to comment, so have to put this as an answer..
As per your code, you will be ended with 'x' having only the last number, to avoid this, you can probably define x as a list and keep appending to it so that you also capture the previous entries.
numbers = int(input("how many numbers do you want? "))
x = []
for i in range(numbers):
x.append(int(input("enter the number " + str(i+1))))

Not entirely sure why this is something you would like but if you need more help, just ask!
numbers = int(input("how many numbers do you want? "))
for i in range(1, numbers+1):
cur = input(f"enter the number {i}: ")
x = int(cur)
# Do something with x?

I recommend using fstrings as below:
numbers = int(input("how many numbers do you want? "))
for i in range(numbers):
x = int(input(f"enter the number {i+1}"))
(you should use i+1 if you want to start from 1)

Related

How can we add inputs when total number of inputs are not known using strings in Python?

I could get the inputs but don't know how to add them.
This is what I tried:
n = int(input("enter the total number to find average"))
for i in range (0, n):
i = int(input("enter the number {i} here"))
if you want to be able to have an arbitrary number of inputs you can use a while loop and a break condition
numbers = []
i = 1
while True:
try:
numbers.append(int(input(f"enter number {i} (enter blank to end):")), inplace=True)
except ValueError:
break
in this way you can store as many or few inputs as the user wishes to provide in a list and then perform the rest of the script on that list
Please try this:
n = int(input("enter the total number to find average"))
s=0 #a varaiable to store sum
for i in range (1, n+1):
i = int(input("enter the number {} here ".format(i)))
s+=i
print('The sum of all the numbers entered by you is :', s)

How to add X amount number from user input then add all of number in python?

how_many_number = int(input("How many number do you want to print? "))
for take_number in how_many_number:
take_number = int(input("Enter number: "))
sum = 0
sum = sum + take_number
print(sum)
Here you go. To take user input we can use a for loop and for each iteration we can add it to our sum using += operator. You can read through the following code to understand it well enough.
number_of_inputs = int(input("How many number to sum: ")
sum = 0 # Initialize the sum variable
for x in range(number_of_inputs): # Repeat the number of inputs times
sum += int(input("Enter Value: ")) # Take a input and add it to the sum
print("Sum is", sum) # print out the sum after completing
You can also compress it into a List Comprehension, like this...
how_many_number = int(input("How many number do you want to print? "))
print(sum([int(input("Enter number: ")) for i in range(how_many_number)]))
i would use the following, note: error handle is not yet incorporated.
num_list = []
### create a function that adds all the numbers entered by a user
### and returns the total sum, max 3 numbers
### return functions returns the entered variables
def add_nums():
while len(num_list) < 3:
user_num = int(input('please enter a number to add:'))
num_list.append(user_num)
print('You have entered the following numbers: ',num_list)
total_num_sum = 0
for x in num_list:
total_num_sum += x
print('total sum of numbers = ',total_num_sum)
add_nums()
Also, please follow the StackOverFlow post guidelines; have your post title as a problem question, it has to be interesting for other devs, and add more emphasis on why you want to add numbers entered by user vs running calc on a existing or new data-frame, need more meat.

Trying to extract an amount of numbers from a list in a range in python 3 using user input as starting point

x = input("How many numbers, between two and six? ")
print("You have selected, " + x + " numbers!")
import random # I would like to only print out the amount of numbers entered in the first line!!!
numbers = list(range(1,48)) # So if a user types six they get six random numbers and so on.
random.shuffle(numbers)
print(numbers)
After shuffling the list you can just pick x numbers from it;
for i in range(0,x):
print(i)
This does of course assume the input is a number, and you've set the input as an integer with x = int(input("text")).
import random
x = input("How many numbers, between two and six? ")
print("You have selected, " + x + " numbers!")
numbers = list(range(1,48))
print([random.choice(numbers) for x in range(int(x))])
random.choice(numbers) is a function that takes a argument that must be list and give you any random number as return value, the for loop is used to run random.choice() function for range defined by x. suppose user enter the value 2 so we have x=2 then our for loop will run 2 times and we have 2 random value as output from our numbers list.
try this:
for x in range(int(x)):
print(random.randint(1, 48))
it will work you can also append into the list if you want.
have you tried this
import random
x = input("How many numbers, between two and six? ")
print("You have selected, " + x + " numbers!")
li = [random.random() for x in range(int(x))]
print(li)

Struggling to get the Sum() function to output one value from a list

olst = []
elst = []
E = int(input("Please enter your first number: "))
O = int(input("Please enter your second number: "))
for OS in range(E,O+1):
if(OS%2!=0):
olst.append(OS)
for ES in range(E,O+1):
if(ES%2==0):
elst.append(ES)
print("Sum of all odd values is: ", sum(olst))
print("Sum of all even values is: ", sum(elst))
This programs intention is to print the sum of all odd numbers between my two integers as well as the even. This is my current code, I'm fairly new to python and tinkering around, open to any criticism and tips. The main issue I'm having is when I run my program both sum(olst) and sum(elst) output the answer multiple times until they reach the correct and final answer. Get the feeling my process is fundamentally flawed somewhere early on but hoping that's not the case!
The last two lines with the print statements should not be indented - otherwise, they are located within the for loop and executed multiple times:
olst = []
elst = []
E = int(input("Please enter your first number: "))
O = int(input("Please enter your second number: "))
for OS in range(E,O+1):
if(OS%2!=0):
olst.append(OS)
for ES in range(E,O+1):
if(ES%2==0):
elst.append(ES)
print("Sum of all odd values is: ", sum(olst))
print("Sum of all even values is: ", sum(elst))
There are also many ways how this code can be optimised (for example, you don't really need the lists elst and olst and can compute the sums in the loop or use comprehensions), but that's a different issue.
You already got your answer above but since you mentioned you are open to tips, this is to provide you just an alternate solution using list comprehensions:
E = int(input("Please enter your first number: "))
O = int(input("Please enter your second number: "))
olst = [i for i in range(E, O+1) if i%2 == 1]
elst = [i for i in range(E, O+1) if i%2 == 0]
print("Sum of all odd values is: ", sum(olst))
print("Sum of all even values is: ", sum(elst))
Additionally, you can replace the middle two lines of the code by
olst = [i for i in range(E, O+1) if i%2 ]
elst = [i for i in range(E, O+1) if not i%2]

Simple python code error

error: Can't convert int to str implicitely
Code:
user_name = input("What is your name? ")
print("Hello {}!, this program is going to ask you to type in a series of numbers (all positive integers)" .format(user_name))
total_numbers = input("How many numbers would you like to add")
for i in range(1, total_numbers):
number = float(input("State a numbers {}: " .format(i)))
total_numbers += number
print("Total number: {}" .format(total_numbers))
MY TASK IS TO: Ask for the user’s name to be entered. It should be stored in a suitable variable (like user_name)
Ask for how many numbers are going to be entered. Store this in a suitable variable too. (like “num_values”)
Then get each of the numbers entered.
You will need to have some way of keeping a total of all of the numbers and at the end, once all numbers have been entered, you need to print out something like..
The issue is in lines -
total_numbers = input("How many numbers would you like to add")
for i in range(1, total_numbers):
number = float(input("State a numbers {}: " .format(i)))
total_numbers += number
You have to convert total_numbers to int to use in range function. Like -
total_numbers = int(input("How many numbers would you like to add"))
for i in range(1, total_numbers):
number = float(input("State a numbers {}: " .format(i)))
total_numbers += number
Also, I see that you are adding numbers to total_numbers inside the loop itself, maybe you instead want to create a new variable , initilize with 0 and add to it instead, Increasing total_numbers itself would give you unexpected results.
Code would be -
total_numbers = int(input("How many numbers would you like to add"))
total = 0.0
for i in range(1, total_numbers):
number = float(input("State a numbers {}: " .format(i)))
total += number
print("Total number: {}" .format(total))

Categories