Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
Create a counter that takes in a number from the user, counts from 0 to that number, tells the user as it counts if each number is even or odd, then sums all the numbers together and prints the sum.
The following code and comments are a starting point. The text following each # is a comment, you will need to replace the comments with the necessary code:
count = 0
sum = 0
num = 0
# Ask user for a number
#All code lines that need to be inside the while loop, need to be tabbed over once.
while (count <= num):
# add count to sum
# Use the following if statement along with the remainder
# operator (modulus, %) to check if a number is even or odd.
if count % 2 != 0:
#print the number is odd
else:
#print the number is even
# update counter for the while loop exit condition
count = count + 1
#print the sum
Using for loop
count=0
sum=0
for i in range(0,n+1):
if (i%2==0):
print("Number is Even")
else:
print("Number is Odd")
sum+=i
print(sum)
Using While loop:
count=0
sum=0
while(count<n):
if(count%2==0):
print("Number is Even")
else:
print("Number is odd")
count+=1
sum+=count
print(sum)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
num=int(input('Enter a number:'))
while num !=0:
if num>0:
if num%2==0 and num%7==0:
print('The number is a multiple of two and seven.' )
num=int(input('Enter a number:'))
elif num%2==0:
print('The number is a multiple of two.' )
num=int(input('Enter a number:'))
elif num%7==0:
print('The number is a multiple of seven.' )
num=int(input('Enter a number:'))
else:
print('The number is not a multiple of two or seven.' )
num=int(input('Enter a number:'))
else:
continue
else:
print('The user did not enter negative numbers.' )
Answer:
Enter a number:
>>> 5
The number is not a multiple of two or seven.
Enter a number:
>>> 6
The number is a multiple of two.
Enter a number:
>>> -8
The question is not very clear but I think I understand what you'd like to achieve.
Your solution is partially correct, but the "continue" inside the else is not followed by a new insertion of "num". So the input "num" is never changed when you insert a negative number.
Also the While-else clause has no sense in this case, if you want to count the negative number inserted you can just put a counter inside the first else clause.
You should change the code and move the "input" inside the while:
num = 1
negative_counter = 0
while num !=0:
num=int(input('Enter a number:'))
if num>0:
if num%2==0 and num%7==0:
print('The number is a multiple of two and seven.' )
elif num%2==0:
print('The number is a multiple of two.' )
elif num%7==0:
print('The number is a multiple of seven.')
else:
print('The number is not a multiple of two or seven.' )
else:
negative_counter = negative_counter + 1
continue
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
N = int(input())
count = N
x = range(1, N +1)
for i in x:
N = i + N
print(N - count)
can someone tell me and explain how this code works? I have been spending minutes looking at this and still cant figure out whats going on, the concept is called "sum of consecutive numbers"
I tried this code and for example when I do 100 it shows 5,050 i want to understand how.
the given code is unnecessarily complicated and confusing. her a reworked version with some comments:
N = int(input()) # get input and convert it to int
total = 0 # variable to hold the running total
for i in range(1, N + 1): # create numbers from 1 upto N using a range
total += i # build sum and store it in var total
print(total) # output total
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So I am trying to compare the first item in a list to the second. I want to find out if the first item is equal to, less than, or greater than the second item.Here is what I have so far. I'm stuck at this part :/
numbers = []
for i in range(0,3):
num = input("Please enter an integer: ")
numbers.append(num)
you have this code that will ask the user for 3 integers, and then you are adding them to the list numbers. You need first to convert then to integers by adding int(input(..))
numbers = []
for i in range(0,3):
num = int(input("Please enter an integer: "))
numbers.append(num)
Now we can start comparing the first and the second number of the list:
if numbers[0] > numbers[1]:
print("the first number is bigger than the second")
elif numbers[1] > numbers[0]:
print("the second number is bigger than the first")
else:
print("the first and the second numbers are equal")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I tried to use boolean instead of 'break' , but I could not manage to do that. Is there a way to edit this code without 'break' ?
Primes:
lower = int(input("Enter the lower bound: "))
upper = int(input("Enter the upper bound: "))
for num in range(lower,upper+1):
if num>1 :
for i in range(2,num):
if(num%i) == 0:
break
else:
print(num, end =", ")
You want to get rid of the break and preserve the same behavior of exiting the inner loop as soon as num % i == 0. This is one way to do it, using a boolean flag and replacing the for with a while so we can add one extra condition for the iteration:
for num in range(max(2, lower), upper + 1):
i = 2
prime = True
while i < num and prime:
if num % i == 0:
prime = False
i += 1
if prime:
print(num, end=", ")
You could replace break with pass. I think that will do what you want.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for guidance on a better way to solve Google's Re-ID problem. Instead of typing out the "Re-ID" description here, I will simply list the pertinent parts:
There are 10,000 "Minions" each of which are assigned an integer randomly between 0 and 10000 inclusive.
The Minion will look up his integer as an index in a string of concatenated prime numbers - i.e. 2357111317192329...
His new "ID" is generated by finding the index value in the string according to the Minion's integer assignment and concatenating the next 4 numbers with it - i.e. if the Minion's integer assignment is 3 his new ID would be 71113 (7 is the value at index 3 of the string, then we add the next 4 digits/characters)
To solve this I:
Utilized (thanks again, SO) a function to identify if a number is prime or not: is_prime()
Generated a string of all prime numbers concatenated together which facilitates a Minion with assignment "10000" plus the next 4 characters.
Created a function to generate the new ID based on the Minion's assignment and the string of concatenated values: answer()
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
primes = ''
for i in range(2,21000,1):
if len(primes) < 10005:
if is_prime(i):
primes = primes + str(i)
else:
break
def answer(n):
re_id = primes[n:n+5:1]
return(re_id)
Areas of possible improvement
The 21000 in my for loop is completely arbitrary. By trial and error I found 21,000 facilitates a Minion integer assignment of 10000 plus the 5 additional digits/characters.
Is it necessary to create this long string of concatenated prime numbers before hand? Could it not be done dynamically based on the Minion ID?
Thanks for your insights. Happy Friday.
I have used this code to complete the challenge..
def solution(b):
bag = "2"
for num in range(0,20500):
if num > 1:
for j in range(2,num):
if (num % j) == 0:
break
elif len(bag) >= 10006:
break
elif j==num-1:
bag += str(num)
break
else:
continue
return bag[b:b+5]