Member Avatar for DEATHMASTER

So I have a couple strings

first = input("Enter a starting integer: ")
last = input("Enter an ending integer: ")
    last = last + 1

How do I make another assignment to a string "n" that I want to take the value of each individual integer in the range? I want to use "n" in a well formatted table to analyze each value as odd, prime, etc.

I would assign the integers to a list inside of variable "n". Now, I didn't exactly understand your entire post but here's what I got:

n = []
first = input("Enter a starting integer: ")
last = input("Enter an ending integer: ")
    last = last + 1
for integer in range(first, last):
    n.append(integer)

Then you use index indices to get each item. So if to print all the items. You would do this.

length = len(n)
for x in range(1, length)
    print (n[x])
Member Avatar for DEATHMASTER

Thanks that's just what I needed.

You're welcome, but it's important to know how it's reading from each item in the list. So if you wanted to see the second item of the list you would do list_name[2], or if you wanted to see the 3rd item do list_name[3] and so on.
P.S.
Once your question is solved, it helps if you click the, Thread Solved button at the bottom of the screen.

Member Avatar for DEATHMASTER

Instead of making another thread I'll just continue posting here (I've got small problems popping up here and there).

For determining whether the number is odd or even I have this:

def dOdd(n):
    # returns True if n is odd and False if not.
    if n == 1:
        return False
    else:
        return True

Now how should I refer to this to print "odd" if the number is odd or "even" likewise? I tried:

odd = dOdd(n)

I'm thinking an if statement here but I'm not sure how I would do that and refer to dOdd(n) at the same time.

First of all, let's use a more accurate way to find an odd number.
Do this:

def isOdd(number):
    if (number % 2) != 0: # The % sign is the remainder of a number 
        return True             # When divided
    else:
        return False

isOdd(3) # run the function, testing if 3 is odd

odd_test = isOdd(3)
if odd_test == True:
    print (number, "is odd")
else:
    print (number, "isn't odd")

For determining whether the number is odd or even I have this:

Just som traning in idle is good to do.

>>> 2 % 2
0
>>> 3 % 2
1
>>> 4 % 2
0
>>> 5 % 2
1
>>> 6 % 2
0
>>> # now you see not so hard
>>> 
>>> # we make a function that print odd/even
>>> def odd(number):
	if number % 2 == 1:
		print 'odd'
	else:
		print 'even'
		
>>> odd(2)
even
>>> odd(3)
odd
>>> #we make a function that return True/false
>>> def odd(number):
	if number % 2 == 1:
		return True
	return False

>>> odd(6)
False
>>> odd(7)
True
>>>
Member Avatar for DEATHMASTER

Ah of course, see I know about the modulus but I never think of these things when I have to actually use them.
Anyways how do you use that for a string? I just get an error

TypeError: unsupported operand type(s) for %: 'list' and 'int'

Again, I'm using "n" as opposed to "number"

So i used python 2.x.
Rember () for print in python3.x print('odd')

Now from idle python 3.x

>>> #Let look at type
>>> first = input("Enter a starting integer: ")
Enter a starting integer: 8
>>> 8
8
>>> type(first)
<class 'str'>
>>> #you se this is a string
>>> #so if you gone compare it to integer convert it
>>> a = int(first)
>>> a
8
>>> type(a)
<class 'int'>
>>> #now it`s a integer
>>> def odd(number):
	if number % 2 == 1:
		print ('odd')
	else:
		print ('even')

		
>>> odd(a)
even
>>>

TypeError: unsupported operand type(s) for %: 'list' and 'int'

So now you know how to check for type.
If not correct type you get a message like you got TypeError.

Member Avatar for DEATHMASTER

That seems to work in python idle but in the code I'm writing in emacs I run it and it says "TypeError: int() argument must be a string or a number"
One major difference in idle was

#
>>> first = input("Enter a starting integer: ")
#
Enter a starting integer: 8
#
>>> 8
#
8
#
>>> type(first)
<type 'int'>

So it's already an int, so I tried your way anyways and I get that error.
I am working with functions so maybe that makes a difference. This is as far as I'm along:

def dOdd(n):
    # Returns True if odd
    if (n % 2) == 1:
        return True
    else:
        return False

def linePrinting(n, odd):
    length = len(n)
    for x in range(1, length):
        print (n[x]), odd

first = input("Enter a starting integer: ")
first = first - 1
last = input("Enter an ending integer: ")
last = last + 1

linePrinting()
n = []
for integer in range(first,last):
    n.append(integer)
    if dOdd(a) == True:
        odd = "even"
    else:
        odd = "odd"

dOdd(n)
linePrinting(n, odd)

...and with that I end up with that error "TypeError: unsupported operand type(s) for %: 'list' and 'int'"

So lets think about what you are doing.

first = int(input("Enter a starting integer: ")) # we make it return a integer
first = first - 1
last = int(input("Enter an ending integer: "))   # we make it return a integer
last = last + 1

print (first) # test print
print (last) # test print

#so now this part work

linePrinting()  # this must have 2 argumet and you have given it 0

n = []
for integer in range(first,last):
    n.append(integer)    # here you making a list
    if dOdd(a) == True:  # you dont have a anywere
        odd = "even"
    else:
        odd = "odd"

# and you cant compare a list for opp/even
# back to idle
>>> def dOdd(n):
    # Returns True if odd
    if (n % 2) == 1:
        return True
    else:
        return False

>>> dOdd(5)
True
>>> dOdd(6)
False
>>> x = [2,3,4,5,6]
>>> dOdd(x)  #try to compare list for even/odd
Traceback (most recent call last):
  File "<pyshell#65>", line 1, in <module>
    dOdd(x)
  File "<pyshell#61>", line 3, in dOdd
    if (n % 2) == 1:
TypeError: unsupported operand type(s) for %: 'list' and 'int' #this can not take a list

So rethink what you are trying to do.

Member Avatar for DEATHMASTER

Ok I fixed the minor problems, but if modulus cannot take a list, how do I evaluate each value for "n"?

So you have first and last user inupt value that you use range on.
Then you have a list with number.

>>> first = 2
>>> last = 9
x = list(range(first, last)) 
[2, 3, 4, 5, 6, 7, 8]

>>># now to find odd/even you can use this
>>> x[::2]
[2, 4, 6, 8]
>>> x[1::2]
[3, 5, 7]
>>>

Or look at this solution make 2 list odd/even based on range given by user.

n_odd = []
n_even = []

first = int(input("Enter a starting integer: "))  #input 2
last = int(input("Enter an ending integer: "))    #input 8

for integer in range(first, last):
    if integer % 2 == 1:
        n_odd.append(integer)
    else:
        n_even.append(integer)

print(n_odd)
print(n_even)

'''my output-->
[3, 5, 7]
[2, 4, 6]
'''
Member Avatar for DEATHMASTER

Hmm that isn't exactly how I want it to work (since this is in a printed "list" not a range list like [#, #]). I managed to get it to print but not evaluated properly like this

def dOdd(n):
    # Returns True if odd
    for n in range(first, last):
        if n % 2 == 1:
            return True
        else:
            return False

def linePrinting(n, odd):
    length = len(n)
    if odd == True:
        odd = "odd"
    else:
        odd = "even"
    for x in range(1, length):
        print (n[x]), odd

But this way it's always printing out "odd" some reason.
I figure I need another function to return the sum of the divisors, but how would that work?

Please test your functions.

>>> def dOdd(n):
    # Returns True if odd
    for n in range(first, last):
        if n % 2 == 1:
            return True
        else:
            return False
        
>>> dOdd(5)
False
>>> dOdd(6)
False
>>> dOdd(2)
False
>>> dOdd(9)
False
>>> 
#So it not so hard to se that it dont work.
>>> n = [2,3,4,5]
>>> odd = 5
>>> def linePrinting(n, odd):
    length = len(n)
    if odd == True:
        odd = "odd"
    else:
        odd = "even"
    for x in range(1, length):
        print (n[x]), odd

        
>>> linePrinting(n, odd)
3
4
5
>>> #so think off does this do what you want?

I am not sure what you want now.
Explain very clear what you want,so it is eaiser to help.

Member Avatar for DEATHMASTER

Thanks anyways, I have run out of time for that part. For other part I need to know how to work with math.sqrt with "n" I get error "TypeError: a float is required." if I simply try "math.sqrt(n). However I think its a similar issue as the last problem I had.

I get error "TypeError: a float is required." if I simply try "math.sqrt(n). However I think its a similar issue as the last problem I had.

>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> #Some useful stuff
>>> print (math.sqrt.__doc__)
sqrt(x)

Return the square root of x.
>>> help(math.sqrt)
Help on built-in function sqrt in module math:

sqrt(...)
    sqrt(x)
    
    Return the square root of x.

>>> #So test it out
>>> n = 9
>>> math.sqrt(n)
3.0
>>> #will it work with float number?
>>> n = 9.5
>>> type(n)
<class 'float'>
>>> math.sqrt(n)
3.082207001484488
>>> #So let make a TypeError
>>> n = [1,2,3]
>>> math.sqrt(n)
Traceback (most recent call last):
  File "<pyshell#110>", line 1, in <module>
    math.sqrt(n)
TypeError: a float is required
>>> #So math.sqrt can take integer/float but not list
>>>
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.