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 6 years ago.
Improve this question
so from this code, that is writen down below, if k is even i must +3 to K number, if it's odd i must multiply it 5 times, i dont know how to show that if k is even, anyone can help me out?
k=int(input('K= ')
if k%2: # (is this correct? if k is even)
k+=3
else:
k*=5
In python3,
k = int(input())
if k%2 == 0:
k = k + 3
else:
k = k * 5
if you are using python2, use raw_input() to take input from the user.
hope this helps.
I think you should really study some basic books about python.
You see, blocks work by indenting in python; and else needs a ":" too:
if k % 2 == 0:
k = k+3
else:
k = k*5
should work better.
Related
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 10 months ago.
Improve this question
Im already done with the part before the highlighted part, but i can't understand the part which is highlighted and i dont want to look at the code.
Something like that:
number = int(input("Your number: "))
while number != 1 :
number = collatz(number)
As I understand, it should be something along the lines:
def collatz(number):
if number // 2 == 0:
print(number // 2)
return number // 2
else:
print(3 * number + 1)
def iterate_program:
number = int(input())
while number != 1:
number = collatz(number)
And then you just call iterate_program.
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
please tell my mistake
i m getting the index of the number whose leftsum=rightsum wrong
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(0,n):
if sum(range(L[0],L[i])) == sum(range(L[i+1],L[-1])):
print(L.index(L[i]))
break
else:
print("0")
For starters, you've used range(a,b), which returns all the numbers from a to b-1
Try this:
L=[]
n=int(input("enter the number of values to be added to the list: "))
for i in range(0,n):
k=int(input("enter the values: "))
L.append(k)
for i in range(1,n):
if sum(L[0:i]) == sum(L[i+1:]):
print(L.index(L[i]))
break
elif i==n-1:
print(0)
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 3 years ago.
Improve this question
a = int(input('Enter the number:'))
list=[]
for i in range(1,a+1):
if (a%i==0):
list.append(i)
print(list)
print(sum(list))
Here in this code, I tried to find out the divisor of a given number, then print the divisors and lastly the sum of the divisors. What I need is to put this as a function to call it for future uses.
You could try something like this:
def my_func(val):
lst = []
for i in range(1,val+1):
if (val%i==0):
lst.append(i)
return lst, sum(lst)
Now you can simply call your function like this:
a = int(input('Enter the number:'))
lst, lst_summ = my_func(a)
print(lst)
print(lst_summ)
Also, it is not good practice to use built in functions as variable names. list is a built in function.
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 years ago.
Improve this question
Okey, I actually don't really know how to print a pyramid in python and even worse I need to get this:
Pyramid of n numbers
The length of the pyramid is asked to the user
Here's the code.
n = int(input("\n Enter the lenght of the triangle.. "))
for i in range(n):
for j in range(n - i):
print(" ", end = " ")
for k in range(i):
print("*", end = " ")
print()
Here is the output
One of the options:
import numpy as np
a = int(input())
def pyram(a):
n = np.zeros(a)
hal = int((a+2)/2)+1
n[:(hal-1)] = range(1,hal)
n[(hal-1):] = np.array(range(1,hal-1))[::-1]
print(n)
for i in range(1,a*2,2):
pyram(i)
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 6 years ago.
Improve this question
I have this pseudocode that I need to translate:
Prompt the user to enter a string and call it s.
Let l be the length of string
For i from 0 upto l-1:
print s[0:i]
For i from 0 upto l-1:
print s[i:l]
Print a closing statement
This is my translation:
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in [0,L-1]:
print (s[0:i])
for i in [0,L-1]:
print(s[i:L])
print("This program is complete!")
main()
However, the code isn't printing correctly. Can someone help me find my error? Thank you.
You say for i in [0,L-1], but [0,L-1] is a list with two elements: 0 and L-1. What you want instead is range(0, L) or range(L):
def main():
s=(input("Please enter a string: "))
L=len(s)
for i in range(L):
print (s[:i])
for i in range(L):
print(s[i:L])
print("This program is complete!")
main()