[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Python guy again: episode 2

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 13
Thread images: 2

File: fallout-4-hacking-600x337.jpg (37KB, 600x337px) Image search: [Google]
fallout-4-hacking-600x337.jpg
37KB, 600x337px
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.
>>
File: raid support.jpg (611KB, 1440x1500px) Image search: [Google]
raid support.jpg
611KB, 1440x1500px
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 this
def 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 faster
nums = (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))
Thread posts: 13
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.