hello everyone, a couple weeks back i came here asking for help on learning basic python and you guys were amazing! i can't thank you enough.
but I've hit another block, I've been trying it for about 2 days and i can't figure it out. currently it keeps saying i have a syntax error and its been the same problem all day, but according to what I've read this is the only way to run (random.randint(x,y)):
heres the problem
#problem 2: generate 100 random numbers, split up evens and odds
import random
import math
def random_count(random.randint(x,y)):
a = 0
b = 0
for i in range (1,101):
if (i%2 == 1):
a = a+1
if (i%2 == 0):
b = b+1
return z
z = random_count(random.randint(1,100))
print ('the number of evens is: ',a)
print ('and the number of odds is: ',b)
Correction: not split evens and odds, declare the number of odds and number of evens.
so 100 random numbers between anywhere from 1 to 10000000 and count the number of evens and odds.
random.() shoudn't go as parameter. you must use in another section of code and then call your function.
also you are not checking the random number but the counter
also next time use code tag to post here, specially if python
tl;dr you dun goofed
>>59671533
Code tag? I'm that new
>>59671658[ code ] YOUR CODE HERE [ / code ]
>>59671418
Started python last week and I could do this easily, you are really overcomplicating it in your code.
>>59671685
im the biggest newfag who ever newfagged on this earth. I'm from over on /tg/ where we dont use anything besides dice.
the [ code ] my code [ / code ] goes in the options field?
>>59671793
Glad to know you're so smart, go apply to NASA they'll appreciate you
>>59671812
No, you are required read the rules and the sticky before posting on /g/.
https://www.4chan.org/rules#g
>>51971506
The sticky explains how to use the code tag.
>>59671876
alright, attempt 2. sorry.import random
import math
def random_count(random.randint(x,y)):
a = 0
b = 0
for i in range (1,101):
if (i%2 == 1):
a = a+1
if (i%2 == 0):
b = b+1
return z
z = rancom_count(random.randint(1,1000))
print ('the number of evens is: ',a)
print ('and the number of odds is: ',b)
import random
RANDOM_NUMBER_COUNT = 100
random_numbers = [random.randint(0, RANDOM_NUMBER_COUNT) for i in range(0, 100)]
evens = [ i for i in random_numbers if i % 2 == 0]
odds = [ i for i in random_numbers if i % 2 != 0 ]
print("Number of Odds", len(odds))
print("Number of Evens", len(evens))
>>59671941
Lots of things wrong
random_count() should either generate its own random numbers or accept a list of numbers of random numbers.
Also the function declaration is wrong.
Do thisdef random_count(numbers):
for number in numbers:
pass
>>59671965
You can use generators if you want to be more memory friendly. I read in some places it also tends to run fasternums = (random.randint(0,N) for z in range(0,100))
evens = (1 for z in nums if z%2==0)
# then the number of evens is
print(sum(evens))