-->

Random Number Generator on Python

User input and the ability to manipulate that input are important elements with any programming language. It’s what separates a good program from a great program, one that allows the user to interact and see the results of that interaction.

RNDNUMGEN.PY

It might be simple but this little piece of code will ask the user for two sets of numbers, a start and a finish. The code will then pluck out a random number between the two sets and display it.

  1. from random import *
  2. print(“\n>>>>>>>>>>Random Number Generator<<<<<<<<<<\n”)
  3. nmb1=int(input(“Enter the start number: “))
  4. nmb2=int(input(“Enter the last number: “))
  5. x = randint(nmb1, nmb2)
  6. print(“\nThe random number between”,nmb1,”and”,nmb2,”is:\n”)
  7. print(x)

More Input

While an easy code to follow, it could be more interesting if you prompt the user for more input. Perhaps you can provide them with addition, subtraction, multiplication elements with their numbers.

If you’re feeling clever, see if you can pass the code through a Tkinter window or even the Ticker window that’s available on Number Guessing Game on Python.

Furthermore, the core of the code can be used in a text adventure game, where the character fights something and their health, along with the enemy’s, is reduced by a random number. This can be mixed with the previous code from Page 90’s Number Guessing Game, where we defined the stats for the adventure game’s character.

You can also introduce the Turtle module into the code and perhaps set some defined rules for drawing a shape, object or something based on a user inputted random value from a range of numbers. It takes a little working out but the effect is certainly really interesting.

For example, the code could be edited to this:

  1. from random import *
  2. import turtle
  3. print(“\n>>>>>>>>>>Random Turtle Image<<<<<<<<<<\n”)
  4. nmb1=int(input(“Enter the start number: “))
  5. nmb2=int(input(“Enter the second number: “))
  6. nmb3=int(input(“Enter the third number: “))
  7. nmb4=int(input(“Enter the fourth number: “))
  8. turtle.forward(nmb1)
  9. turtle.left(90)
  10. turtle.forward(nmb2)
  11. turtle.left(90)
  12. turtle.forward(nmb3)
  13. turtle.left(90)
  14. turtle.forward(nmb4)
  15. turtle.left(90)

Whilst it’s a little rough around the edges, you can easily make it more suitable.


Random Number Generator on Python
Random Number Generator on Python

0 Response to "Random Number Generator on Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel