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 8 years ago.
Improve this question
else:
while int(n2) - 1 != 0:
a = int(n) + int(n)
print("" + str(a))
I need this part of code to times n by n2 without using '*' or '/' and I'm not sure how to change this so that it will work. What do I need to change/add to make this work?
Something like this:
lowest, highest = a, b
if b < a: lowest, highest = b, a
total = 0
for _ in range(lowest):
total += highest
print "a x b = %s" % total
You can use a for loop to add n to ans exactly n2 times:
n = 30
n2 = 2
ans = 0
for i in range(n2):
ans += n
print(ans)
If you need to operate on strings (as in your question), you could use the following example:
n = '30'
n2 = '2'
ans = 0
for i in range(int(n2)):
ans += int(n)
print(str(ans))
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 1 year ago.
Improve this question
This code in under is will let the user enter the number of elements of list and the user need enter the elements ( User will need type number_of_elements_of_list times) and then count how many positive number, negative number, zero number. The code is like this:
the_number_of_elements_of_the_list = int(input())
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
if number_list == 0:
zero_number_count += 1
elif number_list == 1 or number_list > 1:
positive_number_count += 1
elif number_list < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
The code have a problem: The list can not compare like that. It will be error but i don't know how to compare the elements of list. Can you help me solve this problem?
Firstly, as teambob pointed out, add indent to the for-loop block.
Secondly, as DarkKnight pointed out, put the count variables outside the for-loop.
Thirdly, for each iteration, in order to use that value alone, use number_list[i-1] instead of number_list. (The index is i-1 rather than i because the range in your code starts from 1 rather than 0)
The final code would look like:
the_number_of_elements_of_the_list = int(input())
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
if number_list[i-1] == 0:
zero_number_count += 1
elif number_list[i-1] == 1 or number_list[i-1] > 1:
positive_number_count += 1
elif number_list[i-1] < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi this is my code and I don't know why I received this type of error
x = int(input())
n = [int(i) for i in input().split()]
middle = n[int((x - 1) / 2)
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
It produces an error as such IndexError: list index out of range because n has the minimum number of list values entered by the user.
Since your intention was for n to have more values than the minimum number of digits entered by the user, I added .range() to the list iteration.
Here is the modified code:
x = int(input("Put in a number: "))
n = [int(i) for i in range(int(input("Put in another number: ")))]
middle = n[int((x - 1) / 2)]
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
If you have any questions or need clarification, please do not hesitate to ask.
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 writing a program that needs to be able to convert a base10 number to a base64 number, and back again using this alphabet:
"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM. "
I've looked at other stack overflow questions, but none of those solutions work. I would appreciate any help, thanks.
Try this
s = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM. "
def encode(n):
ans = ''
if n == 0:
ans = s[0]
else:
while n:
r, n = n % 64, n // 64
ans += s[r]
return ans[::-1]
def decode(n):
ans, m = 0, 1
for char in n[::-1]:
ans += s.index(char) * m
m *= 64
return ans
print(encode(987654321))
print(decode('VZEnF'))
Output:
VZEnF
987654321
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
Task is : input two numbers that is a diapasone . Then count a quantity of simple numbers into this diapasone.
Please correct my python code below
import math
count = (int, input().split())
for i in count:
n = int(input())
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
print(count)
If you want to count the prime numbers in the closed interval [j, k] (which is given with input()), then you could use
import math
interval = tuple(map(int, input().split()))
count = 0
for n in range(max(2, interval[0]), interval[1]+1):
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
else:
count += 1
print(count)
Explanation:
use map to apply int on each string
use count as a counter variable
use range as an iterator over integers in given interval
start prime number tests with 2, even if lower bound is smaller
count up only if no divisor is found (else clause of for loop)
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
I would need help to write a program that will load the 5 three-digit numbers.
The program should then print a number that has the highest digit of
hundreds, tens and ones.
Here is my code:
max_num = 0
for number in range(1,6):
a = int(input("Enter five three-digit number: "))
s = a//100
d = (a//10)%10
j = a%10
if(s and d and j) > max_num:
max_num = a
print(max_num)
It only prints the first number.
You need to keep the maximum of each digit:
s = d = j = 0
for _ in range(5):
a = int(input("Enter a three-digit number: "))
s = max(s, a//100)
d = max(d, (a//10)%10)
j = max(j, a%10)
print(s*100+d*10+j)
But you might find it easier to keep them as strings and manipulate individual characters.