I am unable to figure out whats wrong in the code and why 6 is not displayed as a perfect number.
following is my code for printing perfect numbers below 10:
s=0
for j in range(10):
for i in range(1,j):
if j%i==0:
s=s+i #for summing up the factors
if j==s:
print(j,'is a perfect number')
else:
print(j,'is not a perfect number')
Output is :
0 is a perfect number
1 is not a perfect number
2 is not a perfect number
3 is not a perfect number
4 is not a perfect number
5 is not a perfect number
6 is not a perfect number
7 is not a perfect number
8 is not a perfect number
9 is not a perfect number
The problem was that you were not resetting s for every integer.
for j in range(10):
s=0
for i in range(1,j):
if j%i==0:
s=s+i #for summing up the factors
if j==s:
print(j,'is a perfect number')
else:
print(j,'is not a perfect number')
(0, 'is a perfect number')
(1, 'is not a perfect number')
(2, 'is not a perfect number')
(3, 'is not a perfect number')
(4, 'is not a perfect number')
(5, 'is not a perfect number')
(6, 'is a perfect number')
(7, 'is not a perfect number')
(8, 'is not a perfect number')
(9, 'is not a perfect number')
You need to reset sum to be 0 in the outermost for loop:
for j in range(10):
s=0
for i in range(1,j):
if j%i==0:
s=s+i #for summing up the factors
if j==s:
print(j,'is a perfect number')
else:
print(j,'is not a perfect number')
This prints the output:
0 is a perfect number
1 is not a perfect number
2 is not a perfect number
3 is not a perfect number
4 is not a perfect number
5 is not a perfect number
6 is a perfect number
7 is not a perfect number
8 is not a perfect number
9 is not a perfect number
Related
I feel stupid for already having to ask questions this early into learning python but the task is this:
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
I have not started on the try/except part yet but with my current code it seems to only be running the full loop once then getting stuck on the largest number check.
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" : break
if largest is None or num > largest :
largest = num
print("large", largest)
if smallest is None or num < smallest :
smallest = num
print("small", smallest)
print("Minimum is", smallest)
print("Maximum is", largest)
The output I get just takes the first number as min and last number as min.
Enter a number: 10
Enter a number: 17
Enter a number: 20
Enter a number: 3
Enter a number: 6
Enter a number: done
Minimum is 10
Maximum is 6
I'm stumped any help would be much appreciated.
You're comparing strings, so it's doing a lexicographical order, where 6 is larger than 10. You need to convert the number strings to actual numbers before comparing:
str_num = input("Enter a number: ")
if str_num == "done":
break
num = int(str_num) # Convert to integer
I have a task for my college in which I must program some very simple logic for multiplying numbers in range like 1 to 5 and than multiply like 1*2*3*4*5. And this way for any input number. For 7 it would be 1*2*3*4*5*6*7.
This is my modest code which is not finished because of no idea how to do it .Help, please..
number = int(input("Enter a number:"))
number += 1
for i in range(1,number):
a = i*(number*
print(a)
Try this:
In [1774]: number = int(input("Enter a number:"))
In [1775]: a = 1
In [1776]: for i in range(1, number+1):
...: a *= i
In [1781]: a
Out[1781]: 120
a's value is 120 which is basically (1*2*3*4*5). Hope this helps.
this my 5th day of learning programming in python. Now I looking solution to print a line which will be long as inputted by user text. For simple program is easy. The three hours of manipulating python commands gave me that what I wanted.
print('Write any word:')
inputted_word = input()
print("-" * int(len(inputted_word)))
print("Your word has got: ",len(inputted_word), "characters.")
Screen:
Write any word:
python
------
Your word has got: 6 characters.
Yes, for me is better to find a solution by myself rather than get it from the web.
However, I would like to make a table (in this case multiplication tables). A user should be input a multiplication number. Ok, this is done.
print('Write any number:')
d = input()
for i in range(1, int(d)+1):
print(i, end=' ')
for j in range(1,11):
print(j, end=' ')
print()
Screen (don't worries about the same numbers):
Write any number:
4
1 1 2 3 4 5 6 7 8 9 10
2 1 2 3 4 5 6 7 8 9 10
3 1 2 3 4 5 6 7 8 9 10
4 1 2 3 4 5 6 7 8 9 10
But how to count the number of characters in 1st line to achieve exactly number to create a line? I presume we need to go out from the loop. So, how to make this? As well as how to make vertical line divided the first column? Any tips, solution for the beginner user, who spent the whole day to reading python documentation and info from the web?
Let's break things up into functions. We want the main part of our program to look like this:
word = get_word() # e.g. "python"
print_table(len(word)) # makes table of 6 characters
So let's write our functions. The first one is exactly what you wrote:
def get_word():
print('Write any word:')
word = input()
print("-" * int(len(word)))
print("Your word has got: ", len(word), " characters.")
return word
For the second function, we can do it similar to your way:
def print_table(n):
for i in range(1, n + 1):
print(i, end=' ')
print('|', end=' ') # vertical column
for j in range(1, 11):
print(j, end=' ')
print()
The way I'd do it personally is using something called a "list comprehension" and the str.join() function to make each row first:
def print_table(n):
# Make a string containing '1 2 3 4 5 6 7 8 9 10'
row = ' '.join(str(x) for x in range(1, 11))
# Print n rows
for i in range(1, n + 1):
print(str(i) + ' | ' + row)
I made a simple program to generate prime numbers but It shows 10 as a prime number. In debugging the for loop incremented i from 2 to 4 in single step when number went from 9 to 10.
for i in range (2, int( number / 2) + 1):
if number % i == 0:
number += 1
i = 2
yield number
number += 1`
I'm trying to make a triangle of number like this one here
1
12
123
12
1
Number of line is chosen by the user. For example if user enters : 5. There must be 5 line (like in the previous triangle). And the number must be an odd number.
Here is what I have done so far but i'm stuck.
oddNumber = int(input("Enter an odd number here:"))
for i in range(oddNumber):
for j in range(oddNumber):
print("", end="")
for k in range(1,i):
print(k, end="")
print("")
The output of that is (if I enter 5):
1
12
123