This question already has answers here:
Convert decimal to ternary(base3) in python
(3 answers)
Closed 6 years ago.
I've tried to make a ternary calculator on Python, with some other functions like hex, bin, and oct with the built-in functions. There isn't one for ternary so I built one.
def ternary(n):
e = n/3
q = n%3
e = n/3
q = e%3
return q
i = int(input("May you please give me a number: "))
print("Binary "+bin(i))
print("Octal "+oct(i))
print("Hexadecimal "+hex(i))
print("Ternary "+ternary(i))
enter code here
But it doesn't work. Why? Where is the problem?
There are a couple of mistakes in your code which others have pointed out in the comments, but I'll reiterate them
You are using regular division when you want to use integer division (in Python 3).
You are returning an integer from your function whereas bin, oct, and hex all return strings.
In addition, your ternary function is incorrect, even with the errors fixed. The best way to write a function for base conversion on your own is with recursion.
def ternary(n):
if n == 0:
return ''
else:
e = n//3
q = n%3
return ternary(e) + str(q)
Related
This question already has answers here:
Python Palindrome
(3 answers)
Closed 2 months ago.
I have tried reversing a given string and then checking it but want to know the actual method to solve such questions without using any built-in functions of python.
Tried using built-in functions in python but expecting to know the code without using them.
z=int(input("Enter number:"))
temp=z
rev=0
while(z>0):
dig=z%10
rev=rev*10+dig
z=z//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
You can use list slice for this problem
num = 1234
reverse = int(str(num)[::-1])
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
int(str(num[::-1]) is nothing but converting the string format to integer .
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.
This question already has answers here:
Ordering of string representations of integers [duplicate]
(6 answers)
Closed 2 years ago.
Here's my code, super new to python. Struggling to understand why if I use < it always thinks is less than even though it will print a higher number. If I use greater than it works just fine. What am I missing? Here's my code, super new to python. Struggling to understand why if I use < it always thinks is less than even though it will print a higher number. If I use greater than it works just fine. What am I missing?
import time
t=time.localtime()
msttime=time.strftime("%H",t)
if(msttime < '2'):
print(msttime)
else:
print("This calculation believes msttime is greater than 2")
This code will give you the expected result:
import time
t = time.localtime()
msttime = time.strftime("%H", t)
if (int(msttime) < 2):
print(msttime)
else:
print("This calculation believes msttime is greater than 2")
The reason is that "18" < "2" lexographically, but 18 > 2 numerically. This is because the lexographical comparison has no regard for the second digit. Since 1 is before 2, the comparison ends there. In the numerical comparison, all digits are accounted for.
This question already has answers here:
Numpy is calculating wrong [duplicate]
(2 answers)
Closed 3 years ago.
I am attempting to solve problem 8 of projecteuler. I am having difficulty understanding exactly why my code is not outputting the correct solution. I am aware that the solution to this problem is above the 32 bit maximum, but I do not know how to allow python to work with numbers above that within my code.
For reference, the original question states : "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
from numpy import prod
f = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
z = list(int(i) for i in str(f))
a1 =[]
start = 0
end = start + 13
while end <= len(z):
a1.append(prod(z[start:end]))
start+=1
end+=1
a = a1.index(max(a1))
print(a1[a]) #prints the product solution
print('---')
dimlen=end-start
newstart = a
newend=a+dimlen
print(z[newstart:newend]) #prints the integers that build the solution
I keep getting the number 2091059712, (the solution is 23514624000)
I think it might be numpy.prod. It might be preserving the input type and wrapping the value. Try using:
def prod(it):
p = 1
for m in it:
p *= m
return p
This question already has answers here:
Round a floating-point number down to the nearest integer?
(12 answers)
Closed 7 years ago.
Is there any way to convert a floating point number in Python to an integer except math.floor()?
I have already tried math.floor(), but I am getting an error says:
Cannot import math
Any other way?
Use the int() function
print int(5.3) # "5"
For more info
You can:
Use int(3.14)
import math then math.floor(3.14) or math.ceil(3.14) (depending on which way you want to round) (you said this doesn't work but I'll leave it for reference)
x = x - x % 1 or x -= x % 1