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
  random.randrange ravioli2929 14 190 Mar-17-2019, 04:49 PM
Last Post: perfringo
  Question about the Random Module Exsul 1 58 Mar-13-2019, 02:06 AM
Last Post: ichabod801
  Random function sh98765 1 55 Mar-11-2019, 02:08 AM
Last Post: ichabod801
  no outcome is printing helpme 2 73 Mar-07-2019, 02:15 PM
Last Post: helpme
  Printing Easter date occurrences samsonite 8 202 Mar-06-2019, 11:49 AM
Last Post: samsonite
  problems with random number/string PrasadPython 3 104 Mar-01-2019, 06:00 PM
Last Post: ichabod801
  dict printing last key,value out of loop anna 7 209 Feb-23-2019, 03:22 AM
Last Post: anna
  How to import random? 1nc1 1 111 Feb-20-2019, 07:19 AM
Last Post: buran
  Help with ValueError using ranint from random. FWendeburg 2 96 Feb-14-2019, 04:22 AM
Last Post: ichabod801
  Checking input is close to random num Tomington 2 122 Feb-11-2019, 09:44 AM
Last Post: Tomington

Forum Jump:


Users browsing this thread: 1 Guest(s)