d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Sigh...can Anyone Help With This? > Python
123Next
Add Reply New Topic New Poll
Member
Posts: 11,316
Joined: Nov 6 2004
Gold: 1,500.03
Mar 9 2014 10:46pm
so far I have this and don't laugh please I know i am noob
Code
n1 = int(input("Enter a whole number "))
while n1 in range(0,10):
if n1 % 2 != 0:
print (n1, " is odd")
else:
print (n1, "is even")

and no it isn't working

Write a python program to do the following:

a. Input positive integer number of more than 5 digits and less than 10 digits.
b. Using a While loop check every digit and calculate the following:
• which digit is odd or even.
• how many digits are even and how many are odd.
• the average of the odd digits, and the average of the even digits.
c. Display as output the result that was calculated from the previous step.


An example of the program output if the number entered is 1234321:

Enter a positive integer: 1234321
1 is odd
2 is even
3 is odd
4 is even
3 is odd
2 is even
1 is odd
There are 3 even digits and 4 odd digits.
Average of even odd digits is 2.00 and the average of even digits is 2.67
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 9 2014 11:01pm
Quote (shenk @ Mar 10 2014 12:46am)
so far I have this and don't laugh please I know i am noob



For this I would accept input as a string, and then you can iterate through the string with a for statement.

Code
for character in user_string:
if int(character) % 2 != 0
...
...


Although you will have to further analyze user input to confirm it is a valid integer (characters 0-9). It may be easier to read in an integer from a integer input function if there is one, then cast to a string to iterate over. If you do not want to use a string you will have to figure out the math to find the number at each place within the number which can be quite cumbersome.

Also I don't even think your while loop is valid python it would help if you actually posted errors you are having. Also your logic would be wrong anyways if it were to work. If you enter 12345 that integer is not in the range of 0 to 10.

This post was edited by AbDuCt on Mar 9 2014 11:03pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 10 2014 01:00am
Quote (shenk @ Mar 9 2014 09:46pm)

a. Input positive integer number of more than 5 digits and less than 10 digits.

test to make sure input is valid:
Code
def IsValidInput (Input_String):
try:
if int(Input_String) > 99999 and int(Input_String)< 1000000000:
return True #the string is an positive integer in the correct range
else:
return False #is an integer, but not in correct range
except ValueError:
return False #the string is not an integer


Quote (shenk @ Mar 9 2014 09:46pm)
b. Using a While loop check every digit and calculate the following:
• which digit is odd or even.
• how many digits are even and how many are odd.
• the average of the odd digits, and the average of the even digits.


store the results in a list (using AbDuCt's idea):
Code
List_to_Record_Digits =[0] * 10 #fills our list from index 0 to 9 with 0's
for i, digit in enumerate(Input):
if int(digit) % 2 == 0:
List_to_Record_Digits[len(Input)-i] = "even"
else:
List_to_Record_Digits[len(Input)-i] = "odd"



Quote (shenk @ Mar 9 2014 09:46pm)
c. Display as output the result that was calculated from the previous step.

well everything you need is stored in that list. For example you can reference the 3rd digit (the 100ths place) as List_to_Record_Digits[3]
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Mar 10 2014 01:22am
You're forced to use a While loop?

Lol @ Azrad using that try

:thumbsup:
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 10 2014 01:30am
Quote (killgoreisleet @ Mar 10 2014 12:22am)
You're forced to use a While loop?
whoops i missed that part, oh well... if you want super cheesy:
Code

while True:
----my for loop here---
break


Quote (killgoreisleet @ Mar 10 2014 12:22am)
Lol @ Azrad using that try
hey its the "pythonic way" :evil:
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Mar 10 2014 01:40am
Quote (Azrad @ Mar 10 2014 01:30am)
whoops i missed that part, oh well... if you want super cheesy:
Code
while True:
    ----my for loop here---
    break
 

hey its the "pythonic way"  :evil:


Stop being so efficient. :cry:
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Mar 10 2014 12:49pm
Code
def iseven(number):
if number%2==0:
return True
else:
return False

x = 0
odd_digits = 0
odd_count = 0
even_digits = 0
even_count = 0

while True:
my_number = input("Enter a whole number: ")
try:
int(my_number)
if(len(my_number) > 5 and len(my_number) < 10):
break
else:
print("Please enter a number greater than 5 and less than 10 digits.")
except ValueError:
print("Sorry, that's not a number.")

while (x < len(my_number)):
if(iseven(int(my_number[x]))):
print(my_number[x] + " is even")
even_digits+=1
even_count+=int(my_number[x])
else:
print(my_number[x] + " is odd")
odd_digits+=1
odd_count+=int(my_number[x])
x+=1

print("There are " + str(even_digits) + " even digits and " + str(odd_digits) + " odd digits.")



print("Average of even digits is " + str(round(even_count / float(even_digits), 2)) + " and the average of odd digits is " + str(round(odd_count / float(odd_digits), 2)) + ".")


e/
output


This post was edited by 0n35 on Mar 10 2014 12:57pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 10 2014 02:22pm
Quote (Azrad @ Mar 10 2014 03:00am)
test to make sure input is valid:
Code
def IsValidInput (Input_String):
    try:
        if int(Input_String) > 99999 and int(Input_String)< 1000000000:
            return True #the string is an positive integer in the correct range
        else:
            return False #is an integer, but not in correct range
    except ValueError:
        return False #the string is not an integer




store the results in a list (using AbDuCt's idea):
Code
List_to_Record_Digits =[0] * 10 #fills our list from index 0 to 9 with 0's
for i, digit in enumerate(Input):
    if int(digit) % 2 == 0:
        List_to_Record_Digits[len(Input)-i] = "even"
    else:
        List_to_Record_Digits[len(Input)-i] = "odd"




well everything you need is stored in that list. For example you can reference the 3rd digit (the 100ths place) as List_to_Record_Digits[3]



Ruby master race.

Code

if (value.to_i.to_s == value).eql? true
odd = 0
odda = 0
even = 0
evena = 0
value.each_char do |digit|
if (digit.to_i.modulo 2).eql? 0
puts "#{digit} is even"
even += 1
evena += digit.to_i
else
puts "#{digit} is odd"
odd += 1
odda += digit.to_i
end
end
end
Member
Posts: 11,316
Joined: Nov 6 2004
Gold: 1,500.03
Mar 10 2014 11:44pm
Quote (0n35 @ Mar 10 2014 07:49am)
Code
def iseven(number):
    if number%2==0:
        return True
    else:
        return False

x = 0
odd_digits = 0
odd_count = 0
even_digits = 0
even_count = 0

while True:
    my_number = input("Enter a whole number: ")
    try:
        int(my_number)
        if(len(my_number) > 5 and len(my_number) < 10):
            break
        else:
            print("Please enter a number greater than 5 and less than 10 digits.")
    except ValueError:
        print("Sorry, that's not a number.")

while (x < len(my_number)):
    if(iseven(int(my_number[x]))):
        print(my_number[x] + " is even")
        even_digits+=1
        even_count+=int(my_number[x])
    else:
        print(my_number[x] + " is odd")
        odd_digits+=1
        odd_count+=int(my_number[x])
    x+=1
   
print("There are " + str(even_digits) + " even digits and " + str(odd_digits) + " odd digits.")



print("Average of even digits is " + str(round(even_count / float(even_digits), 2)) + " and the average of odd digits is " + str(round(odd_count / float(odd_digits), 2)) + ".")


e/
output
http://i.imgur.com/N3LMMCr.png


i love you
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Mar 11 2014 05:34am
Quote (shenk @ Mar 10 2014 11:44pm)
i love you


np, if you have any questions post or pm me :thumbsup:
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll