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
Related
I want to compute the sum of first N even numbers based on the user input N using recursive function.
For example:
Sample Input N: 5
Sample Output: 2 + 4 + 6 + 8 + 10 = 30
I did my code in 2 ways but both of them gave wrong outputs. I'm doing something wrong in the function part sorting number in the loop. So I need some help!
n = int(input("Enter a nmuber: "))
for i in range(1,n+1):
for d in range(0,i+1,2):
print(d)
n = int(input("Enter a number: "))
def get_even(n):
for i in range(1,n+1,2):
d += i
print(d)
You can use the following.
Code
def sum_even(n):
# Base case
if n <= 1:
return 0
if n % 2:
# odd n
return sum_even(n - 1) # sum from next smaller even
else:
# even n
return n + sum_even(n - 2) # current plus sum from next smaller even
# Usage
n = int(input('Enter a nmuber: '))
print(sum_even(n))
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.
my 6th grade teacher asked me to code a program in python that input any amount of a number and it outputs the mean so i tried and theirs a problem with the print statement here's the code.i even tried to replace the float(b) statements to float(1) statements but that didn't work either
n = int(input("Enter number of elements :"))
b = input("\nEnter the numbers :")
sum = (float(b)+float(b)+float(b) / float(n)
print("\nThe mean is -",sum())
Please give space separated input here :
input = list(map(float ,input('Enter number of elements : ').split(' ')))
mean = sum(input)/len(input)
print(mean)
: Output :
Enter number of elements : 10 20 30
20.0
i would do something like this.
I assumed you wanted the arithmetic mean and not the geometric one, so the first thing to do is to get the number of values the user wants to input and store it in the variable n. The you iterate over a range n and you sum the inputs to k.
The you divide k by n.
n, k = int(input("Enter number of elements:")), 0
for i in range(n):
k += float(input("Enter value number {}".format(i+1)))
k /= n
print(k)
if you want to use the "cool" way you can do something like this:
n = int(input("Enter number of elements:"))
k = sum([float(input("Enter value number {}".format(i+1))) for i in range(n)]) / n
print(k)
There is an syntax error and logical error in this code.
You haven't closed the opening bracket, It should be like this
sum = (float(b)+float(b)+float(b)) / float(n)
If you want to calculate the mean of 3 values why are you adding the same value for three times here
(float(b)+float(b)+float(b))
Insted of using same values you can get each values from for loop like this
for i in range(3):
num = float(input())
total += num
I want to calculate the median of a range of numbers I input. The range should be between 1 and n+1 (n is the input in my case). I made some research and saw that you can use the inbuild statistics.median. But I can't make it work.
import.statistics
n = int(input("Please input your number: "))
range_1 = range(1, n+1)
list_1 = list(range_1)
I created a list with all values between 1 and n+1 and now I want to calculate the median.
print(statistics.median(list_1))
This is my error I don't know how to print the median. What am I doing wrong? I can feel that it's really simple. Thank you for your help.
Do it like this.
import statistics
number = int(input("enter the number :"))
range1 = list(range(1, number+1))
median1 = statistics.median(range1)
print("Median of the given range is :", median1)
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)]