Generate a list of random numbers - python

So, the assignment is to ask the user for how many randomly generated numbers they want in a list, and then from that list find the: total(sum), the average, the smallest and the largest number. SOFAR, I am getting an error on line 14 "object of type 'int' has no len()". I get the same responce when using < too.
import random
def main():
randomList = 0
smallest = 0
largest = 0
average = int(input("How may numbers (between 1-100) would you like to generate?: "))
total = 0
if average >= 101 or average <= 0:
print("Invalid input:How may numbers (between 1-100) would you like to generate?: ")
else:
while randomList != len(int(average)):
randomList.append(random.randint(1,101))
randomList=sorted(randomList)
print(randomList)
total = sum(randomList)
average = float(sum(randomList)) / max(len(randomList))
largest = randomList.pop(average)
smallest = randomList.pop(0)
print('The total of all the numbers are ',str(total))
print('The average of all the numbers are ',str(average))
print('The largest of all the numbers are ',str(largest))
print('The smallest of all the numbers are ',str(smallest))
main()

Here is a working version of your code.
import random
def main():
smallest = 0
largest = 0
n = int(input("How may numbers (between 1-100) would you like to generate?: "))
total = 0
if n >= 101 or n <= 0:
print("Invalid input:How may numbers (between 1-100) would you like to generate?: ")
else:
randomList = random.choices(range(1, 101), k=n)
print(randomList)
total = sum(randomList)
average = sum(randomList) / len(randomList)
largest = max(randomList)
smallest = min(randomList)
print('The total of all the numbers are ',str(total))
print('The average of all the numbers are ',str(average))
print('The largest of all the numbers are ',str(largest))
print('The smallest of all the numbers are ',str(smallest))
main()
Explanation
Many errors have been fixed:
Use random.choices for a random list of values of specified length.
You use average in the context of a list of numbers and average value. Make sure you name and use variables appropriately.
Sorting is not required for your algorithm.
I have corrected calculations for average, largest, smallest.
In addition, I advise you get in the habit of returning values via the return statement and leave formatting as a step separate from your calculation function.

how about this:
randomList = [random.integer() for i in range(userinput)]

Related

How do you Multiply numbers in a loop in Python from a list of user inputs

I need to create something that can multiply values no matter how many values there are. My idea is to use a list and then get the sum of the list with the sum function or get the first value of the list and set it as the total and then multiply the rest of the list by that total. Does anyone have a way to do it?
Here was my original idea:
total = 0
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
break
else:
total *= number
print(f"The total is: {total}")
however as you may have guessed it just automatically multiplies it to 0 which equals zero. I would also like the code to work for subtraction and division (Already got Addition working)
thanks!
Thanks to the comments I worked out that the way to do it is by changing the beginning total to a 1 when fixed it is this:
total = 1
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
break
else:
total *= number
print(f"The total is: {total}")
Since you are multiplying, you must start with 1 cause anything multiplied by 0 is 0. And idk why you need sum()
total = 1
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
print(f"The total is: {total}")
break
else:
total *= number

how do I print the smallest and largest number of n random numbers without using lists or any other data structure

The program should read a positive integer n from the user to decide how many number to generate.
The random numbers should be in the intervall [1, 100].
I should then print the average value, smallest value and largest value.
But WITHOUT using lists or any other data structure.
I managed to get the average value but I still need to get smallest and largest. any advise?
here is how my code look like so far
You can use the built in min() and max() functions. Here's a bit of code, should be pretty self explanatory:
import random
n = 20
# set up the values
smallest = 101
biggest = -1
for i in range(n):
x = random.randint(1,100)
# take the smallest of the new random number and the current smallest
smallest = min(x, smallest)
# take the biggest of the new random number and the current biggest
biggest = max(x, biggest)
print(smallest, biggest)
If I understand your question, you are trying to print the smallest and largest number of these five numbers without using a list.
You can easily create variables, and change them, if a higher/lower value is generated, then the highest/lowest value before. Hope it helps:
import random
n = int(input("Enter a number of integers to be generated: "))
if n < 0:
print("Please enter a positive integer!")
else:
highest_value = 0
smallest_value = 100
sum_of_random = 0
for x in range(n):
x = random.randint(1, 100)
sum_of_random += x
if x > highest_value:
highest_value = x
if x < smallest_value:
smallest_value = x
print(x, end = " ")
print("\n")
avg = round(sum_of_random / n, 2)
print(avg)
print(highest_value)
print(smallest_value)
If this answers your question, please mark it as a correct answer.

Is it possible to have a user enter integers and add them using a while loop in python?

This is for one of my assignments.
Here is the question just for clarity on what I am trying to do. Please do not give me the answer, just if you could help me understand what I need to do.
Write a Python program that uses a WHILE loop. The program must prompt the user to enter an integer number. The value must be added to a total. The loop must continue until the total exceeds 45. After the loop, the average of the numbers must be calculated. The program must display each of the input values, as well as the sum of all values and the average value.
*** enhancement, replace the user prompt with a random number selector.
This is the current code I am using:
num = int(input('Enter as many integers as you want: '))
numList =num.split()
print('All entered numbers ', numList)
sum = 0
while num >= 45:
print('Sum of all numbers ', sum)
avg = sum / num
print('Average of all numbers ', avg)
This, of course, is not working, I have figured out how to do it with a for loop ( from the internet ) I just cannot seem to understand how to link the input function with the while loop.
You want to read numbers one at a time, until the sum exceeds 45.
total = 0
num_list = []
while total < 45:
num = int(input(...))
num_list.append(num)
total += num
# Now compute the average and report the sum and averages
To make sure the last number is not added to the list if that would put the total over 45,
total = 0
num_list = []
while True:
num = int(input(...))
new_total = total + num
if new_total > 45:
break
num_list.append(num)
total = new_total
The while loop should be used to get values from the user : While the total of the given values is lower than 45, ask the user for another value
numList = []
total = 0
while True:
num = int(input('Enter an integer: '))
if (total + num) > 45:
break
numList.append(num)
total = total + num
avg = total / len(numList)
print('Sum of all numbers ', total)
print('Average of all numbers ', avg)

Take the average and go through the list again. Count the amount of numbers greater than the average and the amount of numbers less than the average

I'm trying to figure out the 2nd half of this question using python 3 but can't seem to figure it out. The first half of the code that I have is the following below...
A = [1,2,3,4]
print(average(A))
2.5
num = int(input('How many numbers: '))
total_sum = 0
for n in range(num):
numbers = float(input('Enter number : '))
total_sum += numbers
avg = total_sum/num
print('Average of ', num, ' numbers is :', avg)
number = int(input("Enter number: "))
if number < 2.5:
print("Your number is smaller than 2.5")
else:
print("Your number is greater than 2.5")
Save the numbers in list. Sort the list and check the number which is equivalent to or greater than an average number. U can then easily find the count of numbers less than and greater than avarage number.

Length list, calculate odds

import math
a = int(raw_input('Enter Average:')) # Average number probability
c = int(raw_input('Enter first number:')) #
d = int(raw_input('Enter last number:')) #
e = 2.71828
for b in range(c,d+1):
x = (a**b)/math.factorial(b)*(e**-a)
odd = round (1/x*0.92, 2)
print odd
For expected number of points in the range from-to, it calculates how much the odds for each correct number of points.
Example : if expected number of points 200 , what is the odd for range 189-192?
I calulated odds for every number in range 189,190,191,192. (43.17,41.0,39.16,37.59)
how to calculate finall odds?
x = 41.0/(41.0/43.17+41.0/39.16+41.0/37.59+1) = **10.03**
But i dont know how to calculate for a list in advance the length of.
If I understand correctly, add all your odds to a list and then
pseudo code:
sum = 1;
for odds in theListOfOdds
sum += (b/odds)
result = b/sum

Categories