Simple python code error - python

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))

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.

How can I accept the user input in such a way that my program recognizes whether it is an int or a float?

can anyone show me how I can accept integer and float for my input (in the list)? Because if I enter numbers in full, the result always comes out with a .0
Here is my code:
user = int(input("How many numbers do you want to sum up: "))
list = [float(input("Number: ")) for i in range (user)]
print("The Sum of the entered Numbers is: ", sum(list))
my output would look like this:
How many numbers do you want to sum up: 3
Number: 2
Number: 1
Number: 5
The Sum of the entered Numbers is: 8.0
You can do:
user = int(input("How many numbers do you want to sum up: "))
list = [float(input("Number: ")) for i in range (user)]
x = sum(list)
if x.is_integer():
x = int(x)
print("The Sum of the entered Numbers is: ", x)

Counting with placeholders

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)

How do i order the numbers a user inputs?

I am basically wondering how i can store the users inputs and put them in order from least to greatest to help them find median mode or range.
from __future__ import division
amtnum = 0
sumofnums = 0
print "Hello, This program will help find the mean of as many numbers as you want."
useramtnum = input("How many numbers will you need to enter: ")#asks user to say how many numbers there are
while amtnum < useramtnum: #Tells program that while the amount of numbers is less than the users input amt of numbers to run.
amtnum = amtnum + 1 #Tells that each time program asks for number add one to amt of numbers
entnum = (int(raw_input("Enter a number:"))) #Asks user for number
sumofnums = entnum + sumofnums #Adds users number to all values
print "The amount of your numbers added up were:", sumofnums
print "The average/mean of your numbers were:", (sumofnums/useramtnum)
put 'em in a list and sort it
mylist = []
while ...
mylist.append(entnum)
mylist.sort()
print mylist
Utilize the basic data structure called a list!
Before your while loop, create a list (aka array)
user_input_list = []
After you get the individual number from the user in the while loop, add the input to the list (in the while loop)
user_input_list.append(entnum)
After the while loop, sort the list (it will sort in place)
user_input_list.sort()
Then the list has every input from the user in sorted order, least to greatest.
To access the last item in the list, use array accessors.
user_input_list[-1]
To access median, utilize the fact you can use the length of the list. Access the length(list)/2 item
user_input_list[int( len(user_input_list) / 2)] #int cast used to remove decimal points
I am by no means a Python expert (as in, this is like my third python program), but I think it's generally easier to call functions, be it in Python or in any other language. It makes code more readable.
These are all basic math functions, so I don't feel I deprived you of knowledge by just writing them. I did, however, leave the range function for you to write. (Since mylist is sorted, I'm sure you can figure it out.)
This allows the user to continually enter numbers and have the mean, median, and mode spit back at them. It doesn't handle non-integer input.
from collections import Counter
from decimal import *
def getMode(l):
data = Counter(l)
mode = data.most_common(1)
print "mode:", mode
def getMean(l):
length = len(l)
total = 0
for x in l:
total = total + x
mean = Decimal(total) / Decimal(length)
print "mean:", mean
def getMedian(l):
median = 0
length = len(l)
if (length % 2 == 0):
median = (l[length/2] + l[length/2 - 1])/2.0
else:
median = l[length/2]
print "median:", median
mylist = []
print "Enter numbers to continually find the mean, median, and mode"
while True:
number = int(raw_input("Enter a number:"))
mylist.append(number)
mylist.sort()
getMean(mylist)
getMedian(mylist)
getMode(mylist)
Solution to solve your problem :
Use a list : More on Lists
Example :
numbers = []
while amount < counter:
...
numbers.append(num)
print sorted(numbers)
You can take a look at modification of your code below.
numbers = []
print "Hello, This program will help find the \
mean of as many numbers as you want."
while True:
try:
counter = input("How many numbers will you need to enter: ")
except:
print "You haven't entered a number."
else:
break
amount = 0
while amount < counter:
try:
num = input("Enter a number: ")
except:
print "You haven't entered a number."
else:
amount += 1
numbers.append(num)
print "The amount of your numbers added up were:", (sum(numbers))
print "The average/mean of your numbers were:", ( (sum(numbers)) / counter)
print "My list not in order:", numbers
print "My list in order:", sorted(numbers)

Categories