-3

I've successfully created a random number generator, and have used the random module before. But I am trying to add a touch of random to my weather simulator project. My issue is that when I add a variable assigned as a random number between 1 and 99, it just wont let me do it.

Here's the code. Can someone help me figure out the right way to do this?

import random
from random import*
t = 100
wmph = 51
h = 25
randwmph = random.randint(1, 99)

print ("\nDynamic Weather Simulator | V.004\n")

while True:

if t >= 0 and t <= 150:

    print("\nHumidity:",h)
    print("\nTemperature:",t)
    print("\nWindSpeed:",wmph)
    print("\n________________\n")

if t >= 5 and t <= 100:
    h = h + 0.6

if h >= 50 and t >= 33:
    print("\nRAINFALL\n")

if h >= 50 and t <= 32:
    print("\nSNOWFALL\n")

if h >= 55:
    h = h - 20
if wmph >= 30:
    t = t - 2
if wmph <= 29:
    t = t + 1

if wmph >= 60:
    wmph = wmph - randwmph # UNABLE TO USE THIS VARIABLE AS A NUMBER, EVEN THOUGH IT IS ASSIGNED AS A RANDOM NUMBER BETWEEN 1 and 99

else:
    wmph = wmph + 4
  • 1
    What do you mean by "won't let me do it" and "unable to use this variable as a number"? Are you getting an error message? See [mcve]. – John Kugelman Jan 13 '18 at 05:28
  • 1
    Please give a proper error description and check your code's indentation and spacing! – Klaus D. Jan 13 '18 at 05:29
  • Welcome to StackOverflow. For future reference please see [How To Ask](https://stackoverflow.com/help/how-to-ask) to increase your chance of getting a meaningful answer. Namely in this case - what is the expected outcome and what is the error encountered. For Python, keep in mind that indentation is important (e.g. are all the codes below the `while` block supposed to be part of the loop? If so, they should be indented. If not: the `while` statement shouldn't be there. – r.ook Jan 13 '18 at 05:33
  • Remove the line `from random import *` or use only `randint`. – iamauser Jan 13 '18 at 05:33
  • 2
    When you `from random import *` you overwrite the module `random` (imported in the previous line) with the function `random.random` from the `random` module. The `random` function doesn't have an attribute `randint`. – kindall Jan 13 '18 at 05:35
  • @kindall You should post that as an answer. I've been looking for a dupe target, but can't find one. – PM 2Ring Jan 13 '18 at 05:43

3 Answers3

1
import random
from random import*

when you use this style for importing,there is no need for random.randint(). You should just use randint(1,99).

Otherwise replace that with just import random and the rest should be fine.

  • 1
    Not necessarily. The `while True:` is going to throw an error unless the code is properly indented. Other than that, yes, removing `from random import*` should fix it. – r.ook Jan 13 '18 at 05:34
  • The code might be indented in OP's editor, one should give him a benefit of doubt that he doesn't know how to indent it on SO – iamauser Jan 13 '18 at 05:36
  • Actually, using `from random import *` is _not_ a good idea. Such wildcard imports aka "star" imports dump all the imported names into your namespace, which is messy and can lead to name collisions. They also make it harder to read the code because you need to know & remember where all the different names come from. Please see https://stackoverflow.com/questions/2386714/why-is-import-bad – PM 2Ring Jan 13 '18 at 05:36
  • I didn't read the rest actually. I just looked at the problem with the random variable since that's what he wanted to know. – Vishnuvardhan Prem Jan 13 '18 at 05:37
  • @iamauser: I'm inclined to err on the side of caution. Too many times I've seen errors caused by careless mistakes like these. Especially for a beginner, these things should be pointed out even if it's probably fine on their side. Just so they don't come back with a second error. – r.ook Jan 13 '18 at 05:37
1

As kindall mentions in the comments, the cause of your error is

from random import*

which imports all the names defined in the random module and dumps them into your global namespace. One of those names is random, which is the random function, and that new name clobbers the previous assignment of that name to the module itself, which was performed by

import random

So now the name random refers to the random function and not the random module. And so when you try to do

random.randint(1, 99)

you'll get this error message:

AttributeError: 'builtin_function_or_method' object has no attribute 'randint'

because the random function doesn't have a randint attribute.


In general, you should avoid such wildcard imports (aka "star" imports). They dump all the imported names into your namespace, which is messy, and as you have discovered, they can lead to name collisions. They also make it harder to read the code because you need to know and remember where all the different names come from. Please see Why is “import *” bad? for further information on this important topic.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Yes, Ive fixed that. Thank you though! It seems like after the fix the code runs, but is not random. Ive edited my original question above..please read and help :) – taylorbeeler Jan 13 '18 at 06:21
  • @taylorbeeler Please don't change your question after it's received relevant answers. Your question and its answers aren't only to help you, they should also be helpful to future readers. But editing it like that makes the existing answers irrelevant. If you have a new question, you can ask a fresh question. – PM 2Ring Jan 13 '18 at 06:27
  • @taylorbeeler However your new problem is because you aren't calling the `random.randint` function inside your `while` loop, so it just keeps using the same original random number that was generated outside the loop. – PM 2Ring Jan 13 '18 at 06:27
  • It says to edit my question if I need to add more details, and so I did. But I understand. And thanks for answering, very much. Is there any way you could help me figure out how to add it to my loop? I was under the impression that it was... – taylorbeeler Jan 13 '18 at 06:30
  • @taylorbeeler Just move the line `randwmph = random.randint(1, 99)` inside the `while` loop. You could put it directly under the `while` line (properly indented), but I suggest putting it just under the `if wmph >= 60:` line, (indented so it's inside the `if` block`) so it only gets generated if you need it. – PM 2Ring Jan 13 '18 at 06:42
0
Replace "random.randint(1,99)" with just "randint(1,99)"

And you HAVE TO indent your while loop. Plus I have added a time delay for better visualization..

import random
from random import*
import time
t = 100
wmph = 51
h = 25
randwmph = randint(1,99)

print ("\nDynamic Weather Simulator | V.004\n")

while True:

    if t >= 0 and t <= 150:

        print("\nHumidity:",h)
        print("\nTemperature:",t)
        print("\nWindSpeed:",wmph)
        print("\n________________\n")

    if t >= 5 and t <= 100:
        h = h + 0.6

    if h >= 50 and t >= 33:
        print("\nRAINFALL\n")

    if h >= 50 and t <= 32:
        print("\nSNOWFALL\n")

    if h >= 55:
        h = h - 20
    if wmph >= 30:
        t = t - 2
    if wmph <= 29:
        t = t + 1

    if wmph >= 60:
        wmph = wmph - randwmph # UNABLE TO USE THIS VARIABLE AS A NUMBER, EVEN THOUGH IT IS ASSIGNED AS A RANDOM NUMBER BETWEEN 1 and 99

    else:
        wmph = wmph + 4
    time.sleep(1)
  • Thanks everyone! I got it to work. It was removing the "from random import*" - I dont even know how I got into the habit of doing that.. And thanks for the time.sleep line!! I had no idea! Definitely helps. – taylorbeeler Jan 13 '18 at 05:45
  • WAIT, it doesnt work. Still. It seems to subtract a specific amount each time, rather than a random number. It's always the same – taylorbeeler Jan 13 '18 at 06:09
  • Add "randwmph = randint(1,99)" line inside while loop. – avaneesh92 Jan 14 '18 at 10:00