Bottom Page

Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from 50 random numbers printing only the sqrt(...)==0
#1
Hey i need to write a program to school and im new to python so im trying different things.
I need to choose 50 random numbers from interval f.e. from 100 random numbers and do number**0.5 and i need it to output the whole numbers that are int after finishing = 25**0.5=5
i have this but honestly its bad af or idk
import random
def main():
    for i in range(50):
        list=random.randint(1,100)
a =[list]
for i in a:
    if i**(0.5)==0:
        print(i)
I dont know what to do because we have like teacher that doesnt know it either so i dont know
Quote
#2
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

First, don't name a variable list. The word list has meaning in python, and if you use it as a variable name, you lose access to that, and so does other code that may depend on it.

On line 4, you assign a random number to list. Fifty times you do this, and every time you are writing over the last one you did. So at the end, list is just a random number, and the variable 'a' only has one number in it. You want to initialize the list, and then append to it in the loop:

numbers = []
for number in range(50):
    numbers.append(random.randint(1, 100)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Max of 3 numbers MrGoat 2 51 Jan-20-2019, 02:11 PM
Last Post: perfringo
  Printing out a triangle using nested for loops MrGoat 12 242 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  3 random numbers MrGoat 1 89 Jan-15-2019, 06:33 AM
Last Post: Larz60+
  Printing class instance P13N 2 118 Dec-23-2018, 10:28 PM
Last Post: P13N
  math.sqrt / pow() rounding small floats, workaround? robertHTompkins 7 226 Dec-22-2018, 10:58 PM
Last Post: robertHTompkins
  random number anasa 2 178 Nov-26-2018, 08:56 PM
Last Post: Excelsiscelia
  Animating Random Walk Excelsiscelia 2 191 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia
  Generate a list of numbers within a list of arbitrary numbers Takeshio 4 221 Nov-08-2018, 12:29 AM
Last Post: Takeshio
  Random Expression Creator SheeppOSU 2 178 Nov-01-2018, 11:08 PM
Last Post: wavic
  Random module Python 3.6 daryl_uk 1 185 Oct-25-2018, 12:09 AM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)