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.
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 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 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 5 years ago.
Improve this question
Why inside for loop is written upper + 1 and not just upper. If I change upper+1 with upper and run the code, I will have the same result!
for num in range(lower,**upper + 1)**
# Python program to display all the prime numbers within an interval
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
It's because range(x,y) stops at y-1. For example:
for i in range(2,5):
print(i)
will give you:
2
3
4
So if you want to check upper as well, you should add 1, so upper+1. Your code returns the same results since upper is not prime, so even if upper is checked (with upper+1 in range), upper will not be printed.
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))
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 8 years ago.
Improve this question
How do I count and PRINT digits that start with triple figures such as 001,002,003,004 etc..
some simple codes will be appreciated!.Thank you
Have a look at the strings format method and the Format String Syntax.
for i in range(1, 4):
print('{0:0>3}'.format(i))
Result:
001
002
003
004
assuming your digits are initially in string format:
x = "001, 002, 003, 004, 005"
y = x.split(", ")
total = 0 # total sum of all the numbers
count = 0 # number of values
for i in y:
total += int(i)
count += 1
print("Total = %d\nCount = %d" % (total, count))
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 9 years ago.
Improve this question
pseudocode:
A = [1,2,3,4,5,6,7,8,9]
B = [4,5,6,7,1,2,6,7,8]
count = 0
for i in range(len(A)):
for j in range(len(B)):
if A[i:i+3] == B[j:j+3]: #check 3 consecutive numbers if are equal
count += 1
print x[i:i+3]
print count
Question: how can I implement when A[4,5,6] == B[4,5,6], then skip to A[6,7,8]==B[6,7,8], instead of A[5,6,7]==B[5,6,7]
You can use a flag variable:
A = [1,2,3,4,5,6,7,8,9]
B = [4,5,6,7,1,2,6,7,8]
count = 0
skip = False #this is a flag variable
for i in range(len(A)):
for j in range(len(B)):
if skip:
skip = False
continue
if A[i:i+3] == B[j:j+3]: #check 3 consecutive numbers if are equal
count += 1
print x[i:i+3]
skip = True
print count