Im sort of new to python, and I have been looking at some tutorials, manuals, etc. and I saw how to find prime numbers, so I thought it would be a good place to start, so I added some stuff to hopefully allow for user input, and it doesnt work.

>>> z=input('please enter the lowest number to search for prime numbers from')
 if z<=1:
  z=2
  print "low value changed to 2"
>>> y=input('please enter the highest number to search for prime numbers to')
>>> for n in range(z, y):
 for x in range(z, n):
  if n % x == 0:
   break
 else:
  # loop fell through without finding a factor
  print n, 'is a prime number'

What am I doing wrong?

Recommended Answers

All 8 Replies

Im sort of new to python, and I have been looking at some tutorials, manuals, etc. and I saw how to find prime numbers, so I thought it would be a good place to start, so I added some stuff to hopefully allow for user input, and it doesnt work.

>>> z=input('please enter the lowest number to search for prime numbers from')
 if z<=1:
  z=2
  print "low value changed to 2"
>>> y=input('please enter the highest number to search for prime numbers to')
>>> for n in range(z, y):
 for x in range(z, n):
  if n % x == 0:
   break
 else:
  # loop fell through without finding a factor
  print n, 'is a prime number'

What am I doing wrong?

Please use editor or IDE to write program, using the shell with the >>> prompts makes it hard to test your program for most of us. Here is my correction:

z = input('please enter the lowest number to search for prime numbers from ')
if z <= 1:
    z = 3
    print "low value changed to 3"
    
y = input('please enter the highest number to search for prime numbers to ')

for n in range(z, y+1):   # range to include y
    for x in range(2, n):   # x starts with 2 !!!!!!!!!!
        if n % x == 0:
            break
    else:
        # else belongs to for, loop fell through without finding a factor
        print n, 'is a prime number'

Sorry about the unwieldy format, and thanks very much.

Ok that works fine, but what is wrong with this program that I just made, it looks like it should work, but it doesn't.

a=input('Do you want to convert Fahrenheit, Celsius, or Kelvin? ')
if a=Fahrenheit:
        f=input('Please enter a Fahrenheit value: ')
        b=input('Do you want to convert to Celsius or Kelvin? ')
            if b=celsius:
                n=(f-32)*5.00/9.00
                print n
            if b=kelvin:
                n=((f-32)*0.55)+273.15
                print n
if a=celsius:
        c=input('Please enter a Celsius value: ')
        h=input('Do you want to convert to Fahrenheit or Kelvin? ')
            if h=fahrenheit:
                m=(c+32)*9.00/5.00
                print m
            if h=kelvin:
                m=(((c+32)*9.00/5.00))*((F-32)*0.5555)+273.15
                print m
if a=kelvin:
        k=input('Please enter a Kelvin value: ')
        g=input('Do you want to convert to Fahrenheit or Celsius? ')
            if g=fahrenheit:
                o=k+273.15
                p=(o+32)*9.00/5.00
                print p
            if g=celsius
                o=k+273.15
                print o

Ok that works fine, but what is wrong with this program that I just made, it looks like it should work, but it doesn't.

a=input('Do you want to convert Fahrenheit, Celsius, or Kelvin? ')
if a=Fahrenheit:
        f=input('Please enter a Fahrenheit value: ')
        b=input('Do you want to convert to Celsius or Kelvin? ')
            if b=celsius:
                n=(f-32)*5.00/9.00
                print n
            if b=kelvin:
                n=((f-32)*0.55)+273.15
                print n
if a=celsius:
        c=input('Please enter a Celsius value: ')
        h=input('Do you want to convert to Fahrenheit or Kelvin? ')
            if h=fahrenheit:
                m=(c+32)*9.00/5.00
                print m
            if h=kelvin:
                m=(((c+32)*9.00/5.00))*((F-32)*0.5555)+273.15
                print m
if a=kelvin:
        k=input('Please enter a Kelvin value: ')
        g=input('Do you want to convert to Fahrenheit or Celsius? ')
            if g=fahrenheit:
                o=k+273.15
                p=(o+32)*9.00/5.00
                print p
            if g=celsius
                o=k+273.15
                print o

Use input() for numbers and raw_input() for strings. I also changed some of your conversion formulas, so the result is right. For testing use 0 degC = 32 degF and -40 degC = -40 degF.

# returns lower case string (eg. kelvin or k)
a = raw_input('Do you want to convert Fahrenheit, Celsius, or Kelvin (f, c or k)? ').lower()
if 'f' in a:
        f = input('Please enter a Fahrenheit value: ')
        b = raw_input('Do you want to convert to Celsius or Kelvin (c or k)? ').lower()
        if 'c' in b:
            n = (f-32)*5.0/9.0
            print n
        if 'k' in b:
            n = ((f-32)*0.55)+273.15
            print n
if 'c' in a:
        c = input('Please enter a Celsius value: ')
        h = raw_input('Do you want to convert to Fahrenheit or Kelvin (f or k)? ').lower()
        if 'f' in h:
            m = (c)*9.0/5.0 + 32  # !!!!  0 degC is 32 degF
            print m
        if 'k' in h:
            m = c+273.15    # !!!!  0 degC is 273.15 degK
            print m
if 'k' in a:
        k = input('Please enter a Kelvin value: ')
        g = raw_input('Do you want to convert to Fahrenheit or Celsius (f or c)? ').lower()
        if 'f' in g:
            o = k-273.15           # !!!!
            p = (o)*9.0/5.0 + 32   # !!!!
            print p
        if 'c' in g:
            o = k-273.15  # !!!! 0 degK is -273.15 degC
            print o

Okay now I see where raw_input is used, thanks a lot for that.=)

In your case f, c or k are unique to fahrenheit, celsius, or kelvin, so you can use

if 'f' in a:

better (more general) would be to look at just the first character

if 'f' == a[0]:

What does the little

lower()

tag do?

I was just playing around with string functions. String.lower() converts all upper case letters in the string to lower case letters.

string1 = "Fahrenheit"
print string1.lower()   # result: fahrenheit

This way the user can enter Fahrenheit or fahrenheit or F or f and the if compare works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.