number = int(input("Please enter a positive number? "))
if number >= 0:
for i in range(1, number + 1):
perfect_squares = i**2
print(perfect_squares, end=" ")
elif number < 0:
print("Error: you entered a negative number")
If I input the number 10 for example, I want it to output "1 4 9" right now it is outputting all of the perfect squares from 1 to 10.
How to a end the loop to only do the numbers up to the inputted number?
Just change the range to stop at square root of the number user entered:
number = int(input("Please enter a positive number? "))
if number >= 0:
for i in range(1, int(number ** 0.5 + 1)):
perfect_squares = i**2
print(perfect_squares, end=" ")
elif number < 0:
print("Error: you entered a negative number")
Output:
Please enter a positive number? 10
1 4 9
try this
number = int(input("Please enter a positive number? "))
if number >= 0:
for i in range(1, number + 1):
perfect_squares = i**2
print(perfect_squares, end=" ")
if perfect_squares > i: # add if statement here
break
elif number < 0:
print("Error: you entered a negative number")
Related
I'm trying to write program that asking the user for positive numbers, if it is an odd number, the software sums all of the odd digits in the number, same for even numbers. After that the software asking non stop for numbers and does the same thing as before, till the user type 0/negative number.
After that the software should print the number with the maximal sum. Sometimes it works and sometimes not.
Code:
def sum_Digits(n):
sum = 0
if n % 2 == 0: #For even numbers
while n>0:
if (n%10)%2 == 0:
sum += n%10
n = n//10
else:
n = n//10
print("sum: " , sum)
return sum
elif n % 2 != 0 : #For odd numbers
while n>0:
if (n%10)%2 != 0:
sum += n%10
n = n//10
else:
n = n//10
print("sum: " , sum)
return sum
def read_Numbers(N):
maX = 0
while N > 0: #while askNum Positive continue summing
suM = sum_Digits(N)
if suM > maX:
maX = N
N = int(input("Please eneter a Natural number: "))
if N <= 0:
return maX
def main():
num = int(input("Please enter a Natural number: ")) #asking the user to enter number
sum_Digits(num)
askNum = int(input("Please eneter a Natural number: "))
maxSum = read_Numbers(askNum)
print("Number with maximal sum: " , maxSum)
main()
Bug:
if user enter a negative number it will continue the loop and print the last entered number
because the program doesn't check for negative numbers
Fix:
I have added a condition if N <= 0:
return maX
this will return the last entered positive number
Bug:
sum_Digits(num)
I have removed this line because it is not needed
because we already call the function sum_Digits in the main function
I'm trying to solve the scenario with these conditions:
Ask the user to enter a number
Count up from 1 to the number that the user has entered, displaying each number on its own line if it is an odd number. If it is an even number, do not display the number.
If the user enters a number that is 0 or less, display error
My codes are as follows and I can't seem to satisfy the <= 0 print("error) condition:
num=int(input("Enter number: "))
for x in range(num):
if x % 2 == 0:
continue
print(x)
elif x<=0:
print("error")
Your solution will be :
num=int(input("Enter number: "))
if num <= 0:
print("Error")
else:
for i in range(1, num + 1):
if i % 2 == 0:
continue
print(i)
You need to print the error before looping from 1 to num because if the value is less the 0 then the loop won't run. I hope you understand.
You have to check the condition num <= 0 as soon as the user enters the number:
num = int(input("Enter number: "))
if num <= 0:
print("error")
else:
for x in range(num):
if x % 2 == 0:
continue
print(x)
I have this Python assigment to complete where I need to write a program that reads in X whole numbers and outputs (1) the sum of all positive numbers, (2) the sum of all negative numbers, and (3) the sum of all positive and negative numbers. The user can enter the X numbers in any different order every time, and can repeat the program if desired. This is what I've got so far:
x = int(input('How many numbers would you like to enter?: '))
sumAll = 0
sumNeg = 0
sumPos = 0
for k in range (0,x,1):
num = int(input("please enter number %i :" %(k+1)))
sumAll = sumAll + num
if num < 0:
sumNeg += num
if num > 0:
sumPos += num
if k == 0:
smallest = num
largest = num
else:
if num < smallest:
smallest = num
if num > largest:
largest = num
print("The sum of negative numbers is: " , sumNeg)
print("The sum of positive numbers is: " , sumPos)
print("The sum of all numbers is: " , sumAll)
count = 0
repeat = input('Would you like to repeat? y/n: ')
repeat = 'y'
while y == 'y':
I'm just a little stuck after this point. Any ideas on what I should do?
A simple while loop would suffice.
run = True
while run is True:
x = int(input('How many numbers would you like to enter?: '))
sumAll = 0
sumNeg = 0
sumPos = 0
for k in range (0,x,1):
num = int(input("please enter number %i :" %(k+1)))
sumAll = sumAll + num
if num < 0:
sumNeg += num
if num > 0:
sumPos += num
if k == 0:
smallest = num
largest = num
else:
if num < smallest:
smallest = num
if num > largest:
largest = num
print("The sum of negative numbers is: " , sumNeg)
print("The sum of positive numbers is: " , sumPos)
print("The sum of all numbers is: " , sumAll)
repeat = input('Would you like to repeat? y/n: ')
if repeat != 'y':
run = False
Example of the output:
How many numbers would you like to enter?: 4
please enter number 1 : 3
please enter number 2 : 2
please enter number 3 : 4
please enter number 4 : 5
The sum of negative numbers is: 0
The sum of positive numbers is: 14
The sum of all numbers is: 14
Would you like to repeat? y/n: y
How many numbers would you like to enter?: 3
please enter number 1 : 2
please enter number 2 : 4
please enter number 3 : 3
The sum of negative numbers is: 0
The sum of positive numbers is: 9
The sum of all numbers is: 9
Would you like to repeat? y/n: n
You just need to place your code inside an outer loop, that might start it all over again if the user wants to repeat.
while True:
# all your current code until the prints
repeat = input('Would you like to repeat? y/n: ')
if repeat is not 'y':
break
I need to do as the title suggests however I have ran into a problem. This is my code so far:
#Input
n = int(input('Enter n: '))
#Prime = 0
p = []
#Loopidty
for i in range(n):
#Ask user to input x
x = int(input('Enter a Number: '))
#Check if prime
if x < 2:
print('The number is not prime.')
else:
for n in range(2, x - 1):
if x % n == 0:
print('The number is not prime.')
break
else:
print('The number is prime.')
p.append(x)
#Answer
Answer = sum(p) / n
print('The answer is: ', Answer)
The issues is when I add the prime numbers to the list it only adds the first number and stops there, how do I combat this?
Many thanks
It's a good idea to break your code into smaller parts. We can create a function to check if a number is prime or not and check that it is correct. After that just add the rest of the code.
import math
def is_prime(x):
"""Check that a number `x``is prime"""
# You only need to go up to sqrt(x)
for n in range(2, int(math.sqrt(x) + 1)):
if x % n == 0:
return False
return True
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sum = 0.0
num_entries = 0 # Number of prime numbers entered by the user
n = int(input('Enter n: '))
for i in range(n):
x = int(input('Enter a Number: '))
if is_prime(x):
sum += x
num_entries += 1
if num_entries == 0:
print("You didn't enter any prime number")
else:
print("The average of the entered prime numbers is ", sum / num_entries)
My goal is to create a program that checks whether a number is a prime number and displays the total number of factors if it is not a prime number.
I have started the code but I realize I need to store my factors in a list in order for the number of factors to be displayed. Here is my (edited) Python code so far:
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
for x in range(1, userNum+1):
if(userNum % x == 0):
factor = 0
numFactors.append(x)
factor = factor + 1
print(userNum,"is not a prime number")
print("Number of factors: ", factor)
break
else:
print(userNum,"is a prime number")
Can someone please tell me how to continue with storing factors into my list (numFactors)? Thank You!
You need to use append()
Your syntax changed as below.
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
for x in range(1, userNum+1):
if(userNum % x == 0):
factor = 0
numFactors.append(x)
factor = factor + 1
...
...
But there is a logical flaw in your code. Try input as 99.
Corrected solution
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
# A prime number (or prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
# Hence range starting from 2
for x in range(2, userNum+1):
if(userNum % x == 0):
numFactors.append(x)
if len(numFactors) == 1:
print(userNum, " is a prime number")
else:
print(userNum,"is not a prime number")
print("Number of factors: ", len(numFactors))
print("Factors : ", numFactors)
Output
Enter a number: 99
User Number: 99
99 is not a prime number
Number of factors: 5
Factors : [3, 9, 11, 33, 99]
First, you need to put the line that defines numFactors, numFactors = [], outside of and before the for loop. Otherwise, it will be continually set to an empty list for each iteration of the loop.
Values can be added to the end of a list by using the list's append method. For example, numFactors.append(2) would add 2 to the end of the list.
Your mistake is lacking check whether x is equal to userNum, that is in the end of the loop.
My other suggestion is you remove variable factor as this can be known from the length of the numFactors list.
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
for x in range(1, userNum + 1):
if userNum % x == 0:
numFactors.append(x)
if userNum == x:
print("Factors: " + str(numFactors))
if len(numFactors) == 2:
print(userNum, "is a prime number")
else:
print(userNum, "is not a prime number")
print("Number of factors: ", len(numFactors))