Tycho 9000
Tycho 9000

Reputation: 17

Python random number generator program not printing results

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

Upvotes: 0

Views: 94

Answers (2)

Floris
Floris

Reputation: 46375

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

Upvotes: 1

Andrew Clark
Andrew Clark

Reputation: 208475

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

Upvotes: 1

Related Questions