How to calculate the average number of random numbers generated? - python

I have written a FOR loop to call a function 100 times to get 100 random numbers, I now need to calculate the average number of all the random numbers generated. How can I do this? This is where I got up to so far
import random
num1 = int(input("Input First number "))
num2 = int(input("Input Second number "))
for i in range(10):
print(random.uniform(num1, num2), end = "\t")

First of all, your function is only being run 10 times. You need to store the values you're generating, rather than printing them to the screen. You can do this by storing it in a list.
To get a list of 100 random variables, you could do [random.uniform(num1, num2) for i in range(100)].
Then, you need to find the average of this. To get a total sum, you can do sum(list). To get the number of values, do len(list). If we combine this all together, we get:
import random
num1 = int(input("Input First number: "))
num2 = int(input("Input Second number: "))
random_numbers = [random.uniform(num1, num2) for i in range(100)]
print(sum(random_numbers)/len(random_numbers))
Output is:
>>> import random
>>> num1 = int(input("Input First number: "))
Input First number: 10
>>> num2 = int(input("Input Second number: "))
Input Second number: 20
>>> random_numbers = [random.uniform(num1, num2) for i in range(10)]
>>> random_numbers
[13.083389212287019, 12.551686149990369, 13.881302022239865, 12.5156539109837, 12.340949073439575, 13.693758114264867, 13.972147752101735, 14.111313446849902, 11.693700678679372, 18.136716333128035]
>>> print(sum(random_numbers)/len(random_numbers))
13.5980616694

import random
rand_nums = [random.uniform(num1, num2) for i in range(10)]
average = sum(rand_nums) / len(rand_nums)
Or if you prefer to use numpy
import numpy as np
rand_nums = np.random.uniform(num1,num2,10)
average = rand_nums.mean()

I think you have to store the numbers somewhere, not print them. If you use a list, you can calculate the sum and then divide by the length.
import random
num1 = int(input("Input First number "))
num2 = int(input("Input Second number "))
numbers = []
for i in range(10):
numbers.append(random.uniform(num1, num2))
print(numbers)
print(sum(numbers)/len(numbers))

This is very basic stuff, hope this helps:
import random
num1 = int(input("Input First number "))
num2 = int(input("Input Second number "))
sum = 0
numbers = 100
for i in range(numbers):
random_number = random.uniform(num1, num2)
sum += random_number
avarage = sum/numbers
print(avarage)

Related

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)

Find greatest number entered by four users

I am learning python. I need to know, is this also valid way to get an output?
Following code should give me highest user input( highest number among the four numbers)
num1 = int(input("Enter number 1: ")) #user 1
num2 = int(input("Enter number 2: ")) #user 2
num3 = int(input("Enter number 3: ")) #user 3
num4 = int(input("Enter number 4: ")) #user 4
allNum = {num1, num2, num3, num4}
print("All numbers: ",allNum)
if(num1 > num2 and num1 > num4) :
print("User 1's number is greater")
elif(num2 > num3) :
print("User 2's number is greater")
elif(num3 > num4) :
print("User 3's number is greater")
elif(num4 > num2) :
print("User 4's number is greater")
else :
print("done")
With your current method of directly comparing each number, you would do something like this:
if num1 > num2 and num1 > num3 and num1 > num4:
...
elif num2 > num1 and num2 > num3 and num2 > num4:
...
...
Luckily, there is an easier way! You can use Python's built-in max() function, which returns the maximum value of a sequence. That would look something like this:
maximum = max(num1, num2, num3, num4)
if num1 == maximum:
...
elif num2 == maximum:
...
...
The above method works fine, but what if you wanted to use 5 numbers? What about 6? 20? The more numbers you add, the more messy it gets. Each additional number of numbers, you would add another elif statement, which gets tedious. So I would also recommend using some loops and a dictionary for gathering, storing, and comparing the user input.
>>> nums = {}
>>> for i in range(4):
nums[i+1] = int(input(f"Enter number {i+1}: "))
Enter number 1: 4
Enter number 2: 2
Enter number 3: 8
Enter number 4: 10
>>> print("All numbers:", list(nums.values()))
All numbers: [4, 2, 8, 10]
>>> for k, v in nums.items():
if v == max(nums.values()):
print(f"Number {k} is greater")
break
Number 4 is greater
>>>
Trying to keep it as simple as possible for you to understand
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))
#Store all numbers in list.
allNum = [num1, num2, num3, num4]
#Get the max value of all numbers in that list
maxNum = max(allNum)
#To get the position of this value, use list.index() function
maxIndex = allNum.index(max(allNum))
#Lets say your numbers are [3,5,4,1]. .index() would give you "1" because the index starts from 0 to length of list -1.
#In this case, it would be 0-3. So, if you want `Number 2` is greater instead, then you just add 1 to the maxIndex.
print("Greatest number is number "+str(maxIndex+1))
You can index and max to solve this.
a = []
for i in range(4):
a.append(int(input(f"Enter number {i+1}: ")))
print(f"Number {a.index(max(a)) + 1} is greater")
This is the code i wrote .. I'm just learning python
n1 = int(input("Enter 1st Number: "))
n2 = int(input("Enter 2nd Number: "))
n3 = int(input("Enter 3rd Number: "))
n4 = int(input("Enter 4th Number: "))
if (n1>n2) and (n1>n3) and (n1>n4):
print("n1 is Greater")
elif (n2>n1) and (n2>n3) and (n2>n4):
print("n2 is Greater")
elif (n3>n2) and (n3>n1) and (n3>n4):
print("n3 is Greater")
else:
print(n4,"is Greater")

I am writing a program which adds numbers inputed by the user and then outputting the total. With the total how can i output all the inputed numbers

For example I am the user and I input 1 then 3 then 7 then 5
I will get the output 16 but how can I also show the numbers I inputed i.e 1,3,7,5
You could simply store all inputs in a list and print them at the end.
inputs = []
#for each input
inputs.append(userinput)
for value in inputs:
print(value)
if you want your numbers to be in the 1,3,7,5 format use
numbers = ",".join(str(value) for value in inputs)
print(numbers)
You can use variable to store user inputs, Below is sample code for sum of two numbers
n1 = int(input())
n2 = int(input())
s = n1 + n2
print(n1, n2, s)
MOST COMMON CODE:
Suppose you have this following code :
num1 = int(input())
num2 = int(input())
num3 = num1+num2
print(num3)
Now,you want num1 and num3 to be displayed,so u just have to do the following,thats it.
num1 = int(input())
num2 = int(input())
num3 = num1+num2
print(num3)
print(num1)
print(num2)
Check this:
>>> inputs = [input('Enter Number [%d]:' % (i+1)) for i in range(4)]
Enter Number [1]:1
Enter Number [2]:3
Enter Number [3]:5
Enter Number [4]:7
>>> print('{}={}'.format('+'.join(inputs), sum(map(int,inputs))))
1+3+5+7=16
a = input('number: ').split()
print (a, sum(int(_) for _ in a))

Taking the sum of a list without sum()

I have a list that gets generated when a user inputs a random number they want. I want to add the sum together with out using sum(). How could I do this?
xAmount = int(input("How man numbers would you like to input: "))
numList = []
for i in range(0, xAmount):
numList.append(int(input("Enter a number: ")))
print(numList)
From here
Store the sum in a temp variable. Keep adding the input numbers to the temp variable:
xAmount = int(input("How man numbers would you like to input: "))
numList = []
numList_sum = 0
for i in range(0, xAmount):
inputted_number = int(input("Enter a number: "))
numList_sum += inputted_number
numList.append(inputted_number)
print(numList)
print(numList_sum)
You don't need a list at all.
xAmount = int(input("How man numbers would you like to input: "))
result = 0
for i in range(0, xAmount):
result = result + int(input("Enter a number: "))
print(result)
To sum a list just iterate over the list, adding up the individual values:
num_total = 0
for value in numList:
num_total += value
You could also use reduce but this might not meet your h/w requirements either:
from functools import reduce
from operator import add
num_total = reduce(add, numList, 0)

If-Else Statement-Positive and Negative Integers

I am just starting my first computer science class and have a question! Here are the exact questions from my class:
"Write a complete python program that allows the user to input 3 integers and outputs yes if all three of the integers are positive and otherwise outputs no. For example inputs of 1,-1,5. Would output no."
"Write a complete python program that allows the user to input 3 integers and outputs yes if any of three of the integers is positive and otherwise outputs no. For example inputs of 1,-1,5. Would output yes."
I started using the if-else statement(hopefully I am on the right track with that), but I am having issues with my output.
num = int(input("Enter a number: "))
num = int(input("Enter a number: "))
num = int(input("Enter a number: "))
if num > 0:
print("YES")
else:
print("NO")
I have this, but I am not sure where to go with this to get the desired answers. I do not know if I need to add an elif or if I need to tweak something else.
You probably want to create three separate variables like this:
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
In your code you only keep the value of the last number, as you are always writing to the same variable name :)
From here using an if else statement is the correct idea! You should give it a try :) If you get stuck try looking up and and or keywords in python.
On the first 3 lines, you collect a number, but always into the same variable (num). Since you don't look at the value of num in between, the first two collected values are discarded.
You should look into using a loop, e.g. for n in range(3):
for n in range(3):
num = int(input("Enter a number: "))
if num > 0:
print("YES")
else:
print("NO")
Use the properties of the numbers...
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
Try Something like this
if num1< 0 or num2 <0 or num3 < 0:
print ('no')
elif num1 > 0 or num2 > 0 or num3 > 0:
print ('yes')

Categories

Resources