In Python you can import other programs so that you can do things without typing in tonnes of code.
One common example of this is for generating random numbers.
The command "import" allows you to utilize code without having to type it into your program.
Look at the following code and try typing it in:
By importing random, you can now use the command:
random.randrange( )
This command generates a random number from 0 to the number in the brackets minus one!
For example to generate a number from 0 to 9 you use:
random.randrange(10)
The reason it the maximum is -1 from the number is that the range is as wide as the number in the brackets starting at 0. So 0 to 9 is (10) spots!
This is why we add the +1. It bumps that range up so it is 1-10 and not 0-9.
So to generate a random number from 1 to 10 you type:
random.randrange(10)+1
If you want to generate a random number in a range (a,b) then you would use:
Note that a cannot equal b!
Assignment #8:
Make a program that generates a random amount of money from $10-$100 and three items with a random price between $1-$10.
You have 4 options in the store:
BONUS: You can do this under 27 lines.