So I am trying to get a program working for my class. I have to create a random number and then have it print off and from there I have to have it list the highest number and the lowest number. I am struggling to figure out how to get that to work.
import random
print("My Favorite Numbers")
for myFavNumbers in range(0,100,10):
myFavNumbers = random.randint(0,100)
print(myFavNumbers)
numSrch = (myFavNumbers)
for myFavNumbers in range(myFavNumbers):
if numSrch[myFavNumbers].upper()==numSrch.upper():
print(myFavNumbers[myFavNumbers])
it is a screenshot of the error
I hope I understood your task correctly. Is highest = max? Is lowest = min?
import random
random_numbers = []
print('My random numbers:')
for _ in range(10):
random_numbers.append(random.randint(0, 100))
print(random_numbers[-1])
print()
print(f'Max one: {max(random_numbers)}')
print(f'Min one: {min(random_numbers)}')
Try this Code:
import random
print("My Favorite Numbers")
listofstud=[]
for myFavNumbers in range(0,100,10):
myFavNumbers = random.randint(0,100)
listofstud.append(myFavNumbers)
print("List of Student:"+str(listofstud))
print("Max Number:"+str(max(listofstud)))
print("Max Number:"+str(min(listofstud)))
this code will give max and min num from list
From what I understand, your assignment is asking you to:
Generate a bunch of random numbers.
Remember those random numbers.
Print out the numbers and find the highest and lowest numbers.
Right now, what your code is doing is:
Generating a bunch of random numbers and printing out each individual number.
Remembering only the last number.
Rather than saving the last number, you need to find a way record each number as it passes by. That is, you need to store the numbers in a list.
Once you've built a list of random numbers, you can use Python's built-in functions to do the rest of the work for you.
So, given all that, see if this code makes sense:
import random
print("My Favorite Numbers")
favoriteNumberList = []
for _ in range(0,100,10):
myFavNumber = random.randint(0,100)
favoriteNumberList.append(myFavNumber)
print(favoriteNumberList)
print(max(favoriteNumberList))
print(min(favoriteNumberList))
Related
I am trying to create a lottery loop which stops when random generated numbers match the winner ones. But I'm getting this after some time.
---------- FINISHED ----------
exit code: -1073741571 status: 1
import sys
import random
sys.setrecursionlimit(1500000)
lotteryWinner = []
lotteryRandom = []
for i in range(6):
number = random.randint(1,50)
while number in lotteryWinner:
number = random.randint(1,50)
lotteryWinner.append(number)
lotteryWinner.sort()
print(lotteryWinner)
def main():
if len(lotteryRandom)>5:
lotteryRandom.clear()
for i in range(6):
number = random.randint(1,50)
while number in lotteryRandom:
number = random.randint(1,50)
lotteryRandom.append(number)
lotteryRandom.sort()
print(lotteryRandom)
if lotteryWinner != lotteryRandom:
main()
if lotteryWinner == lotteryRandom:
print('You win')
main()
The exit code you receive occurs to indicate that the program indeed did recurse till the provided limit, however reaches the maximum number of recursions. Often it is also necessary to provide the threadinglimit. But since the program is based on random number generation, it might just work some time when a matching list is indeed found. However, it is important to understand how small a probability you are dealing with. You are trying to match 5 random numbers in the two lists which includes:
You want to generate the exact same 5 numbers.(from 1 to 50, where the chaces of picking 1 number is 1/50 and the probability of picking 5 numbers is (1/5)^5)
You want them in the same order in the list. (sorting them as you have done is a good choice)
To make the chances better, one of the multiple things you could do is
import sys
import random
sys.setrecursionlimit(1500000)
lotteryWinner = []
lotteryRandom = []
for i in range(6):
number = random.randint(1,10)
lotteryWinner.append(number)
lotteryWinner.sort()
print(lotteryWinner)
def main():
number = random.randint(1,10)
if number not in lotteryWinner:
main()
else:
print('You win')
main()
Output:
[3, 4, 6, 9, 10, 10]
You win
To improve the chances, the range for random integer generation has been reduced and the program only checks if the generated number is in the initial list of generated numbers. You could increase the range for random number generation to make winning more rare.
I am working on a game of bingo for which I am using an array with 75 numbers. After rolling a number the code will remove it. I want to stop rolling numbers when there are no numbers left in the array.
So far I have this:
import random
numbers=[]
number=1
for i in range (0,75):
numbers.append(number)
number=number+1
How do I get the code to stop rolling a number when the array runs out of numbers?
You can use random to shuffle your elements, then pick them in order:
import random
numbers = list(range(1,76))
random.shuffle(numbers)
while len(numbers) > 0:
number = numbers.pop()
print(number)
You can use the random function to mix up the outcomes.
You can then use create a sequence which then allows you to pick them in order.
This is what you need:
import random
current_list = list(range(0,75))
while current_list:
value = random.choice(current_list)
print("Current Value is {}".format(value))
current_list.remove(value)
import random
def randnums():
for count in range(6):
number = random.randint(1,9)
print(number)
randnums()
From here I am supposed to add the 6 random numbers and display their total... Any help? Thanks in advance.
Here's your completed homework:
import random
def randnums():
numbers = []
for count in range(6):
number = random.randint(1,9)
numbers.append(number)
print(number)
print(sum(numbers))
randnums()
Don't just print the numbers: accumulate them in a list. Then use the built-in sum function to add the entire list.
For your own efficiency, please get used to searching the official documentation and on-line tutorials for help on language mechanics and tools.
You can just keep a running total of the random numbers rather than storing them in a list and then calling sum() on it. Using a list consumes more memory but is useful if you need to access the numbers later on.
import random
def randnums():
total=0
for count in range(6):
number = random.randint(1,9)
total+=number
print(total)
randnums()
import random
a = random.sample(range(1, 12), 8) # generate your random array of numbers
print a #print the array to visualize the numbers generated
print sum (a)
I'm very new to Python and I hope for some help or guides by asking here.
Here's the problem:
Write a program that estimates the average number of drawings it takes before the user’s numbers are picked in a lottery that consists of correctly picking six different numbers that are between 1 and 10. To do this, run a loop 1000 times that randomly generates a set of user numbers and simulates drawings until the user’s numbers are drawn. Find the average number of drawings needed over the 1000 times the loop runs.
I tried to create something (below), but I just can't think of how to get those average number. Also it seems the loop is not good. Any help or solution? thank you in advance.
from random import randint
from random import choice #???
userlist = []
for y in range(6):
user = input("Enter your entry no.{} lotto number: ".format(y+1))
userlist.append(user)
x = 0
randomlotterylist = []
while not x>1000:
lottery = []
for i in range (6):
lot.append(randint(1,10))
randomlotterylist.append(lottery)
x = x + 1
#Next.. ????
First, you want to know your theoretical average number of drawings, that's (1/10)^6 assuming no repetition allowed. Therefore on average every 1,000,000 tries you'd hit the correct number. That is only if the order matters but I assume in your case the order does not matter, so def your average is less than that...
from random import randint
def number_of_tries_before_hitting_jackpot(user_number):
n_tries = 0
while True:
random_number = set([randint(1,10) for i in range(1,7)])
if user_number == random_number:
return n_tries
else:
n_tries+=1
def ask_user_his_number():
userlist = []
for y in range(6):
user = input("Enter your entry no.{} lotto number: ".format(y+1))
userlist.append(user)
return set(userlist)
n_tries_list = []
for x in range(1,1001):
user_number = ask_user_his_number()
print user_number
tmp = number_of_tries_before_hitting_jackpot(user_number)
print tmp
n_tries_list.append(tmp)
avg_number_drawings = reduce(lambda x, y: x + y, n_tries_list) / len(n_tries_list)
print avg_number_drawings
This code is not what I'd do in the sense that the user needs to input its 6 numbers 1,000 times (v annoying for the user). You could change the ask_user_his_number function to a function that just randomly selects a set of 6 numbers.
by using random module, for loop, list in short without defining fuction.
from random import *
user_num=[]
count=0
for i in range(6):
random_num=randint(1,10)
user_num+=[random_num]
user_num.sort()
for j in range(1000):
picked=[]
for k in range(6):
pick_num=randint(1,10)
picked+=[pick_num]
picked.sort()
if picked==user_num:
count+=1
print("the avg darwing is: ",count, count/1000)
I am using python version 3.
For homework, I am trying to allow five digits of input from the user, then find the average of those digits. I have figured that part out (spent an hour learning about the map function, very cool).
The second part of the problem is to compare each individual element of the list to the average, then return the ones greater than the average.
I think the "if any" at the bottom will compare the numbers in the list to the Average, but I have no idea how to pull that value out to print it. You guys rock for all the help.
#Creating a list
my_numbers = [input("Enter a number: ") for i in range(5)]
#Finding sum
Total = sum(map(int, my_numbers))
#Finding the average
Average = Total/5
print ("The average is: ")
print (Average)
print ("The numbers greater than the average are: ")
if any in my_numbers > Average:
#Creating a list
my_numbers = []
r = range(5)
for i in r:
try:
my_numbers.append(int(input("Enter a number: ")))
except:
print( 'You must enter an integer' )
r.append(i)
#Finding sum
Total = sum(my_numbers)
#Finding the average
Average = Total/5
print ("The average is: ")
print (Average)
print ("The numbers greater than the average are: ")
print ([x for x in my_numbers if x>Average])
I'm sure that you would finally have succeeded in your code even if nobody hadn't answered.
My code is in fact to point out that you should foresee that entries could be non-integer and then to put instruction managing errors of entry.
The basic manner here is to use try except
Note the way I add i to r each time an error occurs in order to make up for the element of r that has been consumed.
Unfortunately, "if any" doesn't work. Take a look at the docs for the any function to se what it does. It doesn't do what you want even if your syntax had been correct. What you want is only the numbers greater than the average. You could use a list comprehension: [number for number in my_numbers if number > Average]. Or you could use a loop.
What you need to do is iterate through the list and print out the numbers above the average:
for i in my_numbers:
if int(i) > Average:
print(i)
To make things easier, you might also want to have the my_numbers list actually start out as a list of integers:
my_numbers = [int(input("Enter a number: ")) for i in range(5)]
Now we can get rid of the map function (don't worry, the hour did not go to waste, you'll find it very useful in other situations as well). You could also go straight to calculating the average without calculating the total, since you won't need the total again.
Average = sum(my_numbers) / 5
This means you can change the for loop so that you can replace int(i) with i.
You could also create a list with a list comprehension, and then print out the values in that list.
aboveAverage = [i for i in my_numbers if i > Average]
for i in aboveAverage:
print i