In this Python code, I have the following problem. When the result is displayed, I get a bunch of zeros in the result (see below) - otherwise, the result is correct (the ending numbers of the string). Can anyone spot the error?
def menu():
binNumber = ''
decNumber = float(input("Enter a positive number: "))
decNumber, binNumber = decimalToBinary(decNumber, binNumber)
printResult(binNumber)
def decimalToBinary(dec, bin):
while dec != 0:
remain = dec % 2
dec = dec / 2
if remain > 0.5:
bin += '1'
else:
bin += '0'
return dec, bin
def printResult(binNumber):
print("The binary notation is:", binNumber[::-1]) # the last part is to reverse the string
menu()
This is the result if I type "2"
The binary notation is:
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010
Change
decNumber = float(input("Enter a positive number: "))
to
decNumber = int(input("Enter a positive number: "))
And use integer division. Instead of
dec = dec / 2
use
dec = dec // 2
with these changes, I get the following output
The binary notation is: 10
#Ragnar, before everything I should mention that your code is wrong. I tried to convert 12 and 41 with this code and it failed. To get a complete help, I suggest you to see this question.
Related
Using the method of looping, write a program to print the table of 9
till N in the format as follows: (N is input by the user)
9 18 27...
Print NULL if 0 is input
Input Description: A positive integer is provided as an input.
Output Description: Print the table of nine with single space between
the elements till the number that is input.
Sample Input:
3
Sample Output:
9 18 27
My solution is:
x = int(input(" "))
if x > 0:
count = 1
while count<=x:
z = 9*count
print(z, end=" ")
count += 1
else:
print("NULL")
Use abs function. So you get only positive values. If you want both positive and negative, you can use normal input function.
I am trying to define a function to convert a binary number to a decimal number and check if it is an absolute square or not. I am passing a list of binary numbers as the argument and the function is supposed to print "True" or "False" as the output in the same order as that of the list elements; depicting whether or not they are absolute squares.
While trying so I am getting a syntax error in the ninth line where I am trying to calculate the decimal equivalent of the binary digits by adding the individual values arising out of each binary digit owing to it's position.
Logic of Execution: 1001 in Binary means [pow(2,3)*1 + pow(2,2)*0 + pow(2,1)*0 + pow(2,0)*1] in Decimal. It's equal to 9 which is an absolute square of 3. So the output should be "True"
import math
n = int(input("Enter the total no of elements to check: "))
num_list = []
for k in range (n):
print("Enter the number at position "+str(k)+" : ")
num = int(input())
num_list.append(num)
#print(num_list) for debugging purpose
def Binary_SquareRoot_Checker(input_list):
for i in input_list:
q = str(i)
no_of_digit = len(q)
#For each element of the list, we need to count the no of digits present
addition_num = 0
for p in range (no_of_digit):
r = q[p]
value = (2**(no_of_digit - (p+1)) * int(r)
addition_num = addition_num + value
#print(addition_num) just to see the decimal number
root = math.sqrt(sum_num)
if int(root + 0.5) ** 2 == sum_num:
#Checking for absolute square property
print("True")
else:
print("False")
Binary_SquareRoot_Checker(num_list)
I am getting Syntax Error at addition_num = addition_num + value
Please tell me why this error is being reported?
In the 20th line, change:
value = (2**(no_of_digit - (p+1)) * int(r)
To:
value = (2**(no_of_digit - (p+1)) * int(r) )
This is my first time using this QnA site... I had this task in my class which asks me to convert numbers from any basis into decimal using method... I'm very confused about converting alphabets contained in a hex number. Here is the program which I type:
def base_n_to_dec(num_string, base):
dec = 0
index = len(num_string) - 1
for digit in num_string:
dec += int(digit) * (base ** index)
index -= 1
return dec
Could anyone please help me to modify this program so it could recognize the alphabets contained in hex numbers, so it could convert the characters to decimal basis? Please don't use built-in functions, because it's not allowed. (I already submitted the task, so I don't mean asking someone to finish my assignment) sorry for the bad english...
alphabet = '0123456789ABCDEF'
def base_n_to_dec(num_string, base):
'''
:param num_string: string containing number in base10
:param base: integer between 2 and 16
:returns: num_string based base as a string
'''
b10_num = 0
for digit in num_string:
b10_num *= 10
b10_num += alphabet.index(digit)
if b10_num == 0:
return '0'
result = ''
while b10_num > 0:
result = alphabet[b10_num % base] + result
b10_num /= base
return result
OK so this is my code, it's a base calculator that will convert base to base with no problem, but once the answer is over 9, I want the number to be represented as a letter, just like in base 16, 10 represent 'a', so I'm stuck on how can I do that just using Ascii tables. Right now the code is running well, if I type 1011,base2, I want to convert to base 16. So the output turns out to be 11, which is correct, but I want it to be 'b'
number = input("what's your number: ")
o_base = int(input("what is your oringal base: "))
base = int(input("what's the base you want to convert: "))
answer = ""
total = 0
for i, d in enumerate(reversed(number)):
total = total + int(d) * o_base ** i
while (total != 0):
answer += str(total % base)
total //= base
print (answer)
Python can first convert your number into a Python integer using int(number, base). Next all you need to do is convert it into you target base. This is often done using divmod() which combines doing a division of two numbers and getting their remainder into one function.
To convert each digit into the correct number or letter, the easiest way is to create an string holding all of your required symbols in order, and then use an index into this to give you the one you need, e.g. digits[0] here will return 0, and digits[10] would return A.
import string
number = input("What's your number: ")
o_base = int(input("What is your original base: "))
base = int(input("What is the base you want to convert to: "))
number = int(number, o_base)
digits = string.digits + string.ascii_uppercase # 0 to 9 + A to Z
output = []
while number:
number, remainder = divmod(number, base)
output.insert(0, digits[remainder])
print(''.join(output))
This example works up to base 36 (i.e. 0 to 9 and A to Z). You could obviously extend digits to give you more symbols for higher bases.
Using a list to create the output avoids doing repetitive string concatenation which is not very efficient.
How do I display the number of one's in any given integer number?
I am very new to python so keep this in mind.
I need the user to input a whole number.
Then I want it to spit out how many one's are in the number they input.
i am using python 3.3.4
How would I be able to do this with this following code?
num = int(input ("Input a number containing more than 2 digits"))
count = 0
for i in range (0):
if num(i) == "1":
count = count + 1
print (count)
I don't know what i'm doing wrong
it gives me 'int' object is not callable error
Something like this:
Int is not iterable so you may need to convert into string:
>>> num = 1231112
>>> str(num).count('1')
4
>>>
str(num).count('1') works just fine, but if you're just learning python and want to code your own program to do it, I'd use something like this, but you're on the right track with the code you supplied:
count = 0
for i in str(num):
if i == "1":
count = count + 1 # or count += 1
print(count)
Just to help you, here is a function that will print out each digit right to left.
def count_ones(num):
while num:
print(num % 10) # last digit
num //= 10 # get rid of last digit
num = 1112111
answer = str(num).count("1")
num = int(input (" Input a number to have the number of ones be counted "))
count = 0
for i in str(num):
if i == "1":
count = count + 1 # or count += 1
print (' The number of ones you have is ' + str(count))
So i took the user input and added it to the correct answer since when i tried the answer from crclayton it didn't work. So this works for me.