0

I made a small program that is designed to generate a random number between 0 and 1, and based on what it is print out a statement (In this case "Missed", "1 point" or "3 points") as well as the number that was actually made. The program generates the numbers fine, but I can't get it to print out the statements, is there something I overlooked here?

def zeroG():
 from random import *
 n=random()
 #print n
 if(0>=n>0.3):
   print("miss")
 elif(0.3>=n>0.7):
   print("1 point")
 elif(0.7>=n>=1):
   print("3 points")
 print n
1
  • if you're only using random.random, just from random import random. Commented Dec 11, 2013 at 5:08

2 Answers 2

1

You need to change all of your > to <. Consider the first if statement, 0 >= n > 0.3 is equivalent to 0 >= n and n > 0.3, or in words "n is less than or equal to zero and greater than 0.3". Of course what you actually want is "n is greater than or equal to zero and less than 0.3", which is 0 <= n < 0.3.

Full code:

from random import random

def zeroG():
  n = random()
  if 0 <= n < 0.3:
    print "miss"
  elif 0.3 <= n < 0.7:
    print "1 point"
  elif 0.7 <= n <= 1:
    print "3 points"
  print n
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou, I've been looking at this code for ages now and missed that completely
1

Look at your conditions: they are backwards. You need

if(0<=n<0.3):
   print("miss")
 elif(0.3<=n<0.7):
   print("1 point")
 elif(0.7<=n<=1):
   print("3 points")
 print n

1 Comment

And don't forget to set seed for random

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.