Problem with randint

This is the place for queries that don't fit in any of the other categories.

Problem with randint

Postby pirranasaurus » Tue Jul 30, 2013 7:46 pm

I'm very new to python. I have recently purchased a raspberry pi and I am trying to teach myself python.
I've been working through a few codes in the 'Haynes Raspberry Pi Manual' which has a section on python.

The begging of the program is:

Code: Select all
from random import *
number = randint(low,high)
shuffle(list)


The code is copied out of the book and I've checked loads that I've types it in correctly. It goes on to produce a random set of insults based on the rude frenchman from monty python.

I get the following error message:

Code: Select all
Traceback (most recent call last):
  File "insult.py", line 3, in <module>
    number = randint(low,high)
NameError: name 'low' is not defined


------------------
(program exited with code: 1)
Press return to continue


I thought that the limits for the randint were in the module? If not, how do I define it? Will I need to do the same for 'high'? There is nothing in the book to say that this needs to be defined. I have tried to input numbers, but that seems to give me more errors elsewhere.

Thanks in advance...
Last edited by Yoriz on Tue Jul 30, 2013 8:00 pm, edited 1 time in total.
Reason: sorted code tags
pirranasaurus
 
Posts: 3
Joined: Tue Jul 30, 2013 7:29 pm

Re: Problem with randint

Postby Yoriz » Tue Jul 30, 2013 7:51 pm

You just set those values to whatever you want it to be, if you wanted it to be between 5 and 10 it would be
Code: Select all
randint(5, 10)

insult is the name of your file
NameError: name 'low' is not defined is saying you have not assigned a value to low
you could also do it like this by asigning a value to low and high
Code: Select all
low = 5
high = 10
randint(low, high)


Also just noticed you have [] around your code for some reason - edit ok it just code tags gone wrong ill sort that in your post
dont do
Code: Select all
from random import *

use
Code: Select all
from random import randint
randint(5, 10)

or
Code: Select all
import random
random.randint(5, 10)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1018
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Problem with randint

Postby pirranasaurus » Tue Jul 30, 2013 9:39 pm

Thank you for your help. I think II understand how to use the randint module properly. This has now left me with another problem. I'll post the full code below:

Code: Select all
from random import randint
randint(1, 10)
suffle(list)

relatives = ["mother", "father", "brother", "sister", "aunt", "nephew", "neice", "grandad", "gran"]
pets = ["dog", "cat", "hamster", "rat", "pidgeon"]
bad_adjectives = ["limp", "fat", "rank", "gasseous", "rotund", "stinking", "boring", "lame"]
insulting_nouns = ["twit", "nonce", "fathead", "skankbucket", "larry", "looser", "douche"]
smelly_things = ["sweaty balls", "stinking bishop", "poo", "urine", "arm pits", "nappies"]
bad_property = ["a limp handshake", "massive kneecaps", "hairy ears", "pig nose", "webbed feet"]

def anyof(things):
   choice = randint(0, len(things)-1)
   return things[choice]

def sentence(words, period):
   return words[0].upper() + words[1: ] + period
   
def article(noun):
   if noun[0] in "aeiou":
      return "an "+noun
   else:
      return "a "+noun
      
def concatenation(things, delim, lastdelim):
   if len(things) == 0:
      return ""
   elif len(things) == 1:
      return thnkgs[0]
   elif len(things) == 2:
      return lastdelim.join(things)
   else:
      return delim.join(things[:-1]) +lasdelim + things[-1]
      
def adjectival_phrase(adejectives):
   shuffle(adjectives)
   description = adjectives[0:randint(1,3)]
   return concatenation(description, ", ",
   " and ")
   
def insult():
   noun = anyof(insulting_nouns)
   person = anyof(["you", "your", +anyof (relatives+pets)])
   if person == "you":
      is_verb = "are"
      smells_verb = "smell"
      has_verb = "have"
   else:
      is_verb = "is"
      smells_verb = "smells"
      has_verb = "has"
   choice = randint(1, 4)
   if choice == 1:
      return person+" "+is_verb+" "+article(noun)
   elif choice == 2:
      return person+" "+is_verb+" " + \
         adjectival_phrase(bad_adjectives)
   elif choice == 3:
      return person+" "+smells_verb+" of " + \
         anyof(smelly_things+pets)
   elif choice == 4:
      return person+" "+has_verb+" " + anyof(bad_property)
   else:
      return "Ooops ... I can't think of anything"

def rudeness():
   choice = randint(1, 4)
   if choice == 4:
      return sentence(anyof(bad_adjectives),"!")
   else:
      insults = []
      for n in range(randint(1,3)):
         insults += [insult()]
      return sentence(concatenation(insults,", ", " and "), ".")

def main():
   print (rudeness())
   
if __name__ == '__main__':
   main()



I know get one of two errors, the first with the shuffle(list) not being defined

Code: Select all
Traceback (most recent call last):
  File "insult.py", line 4, in <module>
    suffle(list)
NameError: name 'suffle' is not defined


If I take out the shuffle( list) I get

Code: Select all
Traceback (most recent call last):
  File "insult.py", line 84, in <module>
    main()
  File "insult.py", line 80, in main
    print (rudeness())
  File "insult.py", line 74, in rudeness
    insults += [insult()]
  File "insult.py", line 44, in insult
    person = anyof(["you", "your", +anyof (relatives+pets)])
TypeError: bad operand type for unary +: 'str'


Sorry if I'm being a massive dunce :?
pirranasaurus
 
Posts: 3
Joined: Tue Jul 30, 2013 7:29 pm

Re: Problem with randint

Postby stranac » Tue Jul 30, 2013 9:47 pm

The first is missing an 'h', and I believe the second has an extra '+'.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Problem with randint

Postby pirranasaurus » Tue Jul 30, 2013 9:52 pm

Just checked that input, still get error:

Code: Select all
Traceback (most recent call last):
  File "insult.py", line 4, in <module>
    shuffle(list)
NameError: name 'shuffle' is not defined

pirranasaurus
 
Posts: 3
Joined: Tue Jul 30, 2013 7:29 pm

Re: Problem with randint

Postby Yoriz » Tue Jul 30, 2013 9:57 pm

Its because you have not imported shuffle
Do your imports like this now you are using more that just the one thing from the module random
Code: Select all
import random
random.randint(5, 10)
random.shuffle(list)


Here is a link why
Code: Select all
from something import *
is bad
Last edited by Yoriz on Tue Jul 30, 2013 10:05 pm, edited 1 time in total.
Reason: Added link
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1018
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Yoriz and 2 guests