Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Need some help with a bit of code for an assignment.
Keep this in mind please, I am new to Python, I've only been at it a month. So I may need some things "dumbed" down to my level please.

Hi everyone reading this. I apologize that I'm here posting the same question again but I figure I should ask it again, however I do have some code here and I have another question too, with code for that one as well.

In my previous question, I asked about how to make a function for converting decimals given by a user, to hexadecimal numbers.

This is a part of my assignment along with the other question that I need help with, my job is to create three functions, one that changes the decimal given by the user to a hexadecimal, a function that takes any digits from 10 to 15 as 'A', 'B', 'C', etc. Along with that I have to use the main function, which calls the other two functions and prints out something like "given_decimal, 'as a hexadecimal, would be', hexadecimal_number"

This hexadecimal number needs to be created by adding each digit as a string too, and I have to make sure in the first function that it takes each quotient and continues to divide it by 16 and give me the remainder for the hexadecimal value.

Here is my code for it:

#Change user input into hexadecimal.
def decToHex(decimal_number):
    hexStr = ''
    while decimal_number != 0 :
        decimal_number = decimal_number // 16
        remainder = decimal_number % 16
    return remainder

#Change hexadecimal digit into character.
def getHexChar(dec_digit):
    stringA = 'A'
    stringB = 'B'
    stringC = 'C'
    stringD = 'D'
    stringE = 'E'
    stringF = 'F'
    if dec_digit < 10 :
        return str(dec_digit)
    elif dec_digit == 10 :
        return stringA
    elif dec_digit == 11 :
        return stringB
    elif dec_digit == 12 :
        return stringC
    elif dec_digit == 13 :
        return stringD
    elif dec_digit == 14 :
        return stringE
    elif dec_digit == 15 :
        return stringF

#Main Program.
def main():  #obtain user input for decimal.

    userInput = int(input('Please enter a decimal value. '))
    #call decToHex.

I'm not sure what I would be able to do to fix this so if anybody can help me understand it as I am a visual learner, not necessarily a tactile learner, that would be great. Thank you.

As for the second question, it requires me to use four functions. One that checks to see if a number in a list of numbers (The user inputs how many numbers to check that are prime, and palindromic) is prime. The second one checks to see if it is palindrome, the third checks to see if a number can be read the same in reverse (I suppose the same as checking if it is a palindrome), and the fourth one to call it all back and print out the numbers that are both prime and palindromic, the main function.

Here is my code for that one:

#Check to see if numbers are prime.

userInput = int(input('Please enter the amount of numbers to be checked. '))

for num in (2, userInput + 1) :
    if num >= 2 :
        for y in range(2, num) :
            if not (x % y) :
                return False
    else :
        return False
    return True

#This is just a sample to test if a number is a palindrome.

n = int(input('Please enter a number. '))
temp = n
reverse = 0
while (n < 0) :
    dig = (n % 10)
    reverse = (reverse * 10 + dig)
    n = (n // 10)
if (temp == reverse) :
    print('This number is a palindrome.')
else :
    print('This number is not a palindrome.')

I attempted to make them work without functions just to try and implement them later, but I'm not quite sure if what I'm doing here is even right, so if anyone can help me work it out here then that would be fantastic as well, thank you.

I have a third question as well but I'd like to learn about these first two before doing the last one and attempt it on my own first. If I have problems with it of course I will post it here with my code to see if I can gather some help.

Thank you to all who respond in advance.
Larz60+ wrote Feb-09-2019, 03:05 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I have fixed for you this time, please use BBcode tags on future posts. Thank You
You're on a viable track for the first problem. There are a few things to change.

First, getHexChar() can be greatly improved with a dict:

def getHexChar(dec_digit):
    letters = {
        10: "A",
        11: "B",
        12: "C",
        13: "D",
        14: "E",
        15: "F"
    return letters.get(dec_digit, dec_digit)
The dict.get() method will check for a key matching dec_digit in letters and, if there is no match, it will return the default which is set to str(dec_digit).

Now, decToHex() has a couple significant issues. The number 16 represented in hex is 10. If we run 16 through decToHex() we would have these results at the end of the first loop:

Quote:16 // 16 == 1
16 % 16 == 0

This indicates that 16 is evenly divisible by 16 exactly once; which is also what the hexadecimal 10 indicates as well. To accomplish the task then, it's evident that the quotient needs to be converted to hex. Also, the function must return the hexStr since it stores the end value:

def decToHex(decimal_number):
    hexStr = ''
    while decimal_number != 0 :
        decimal_number = decimal_number // 16
        remainder = decimal_number % 16
        hexStr += getHexChar(decimal_number)
    return hexStr
Testing this with 155 (hex 9B) returns 90. We're close but there's still more work to do. The problem is that now we are only examining decimal_number and ignoring the remainder. If we take a look at the math again, we see:

Quote:155 // 16 == 9
155 % == 11 <- 11 == 0xB

As noted, the hex for 155 is 9B. Our result is 90 and the remainder that's ignored equals B, which is exactly what we're missing. Looking at the structure of the function again, we need a way to consider the remainder. So, let's add in some logic to check for value.

def decToHex(decimal_number):
    hexStr = ''
    while decimal_number != 0 :
        decimal_number = decimal_number // 16
        remainder = decimal_number % 16

        if decimal_number == 0:
            hexStr += getHexChar(remainder)
            hexStr += getHexChar(decimal_number)
    return hexStr
It still gives us 90 instead of 9B for 155. The issue is with the remainder calculation. Because decimal_number gets reset before the modulus is calculated, it's doing this:

Quote:loop 1:
155 // 16 == 9
9 % 16 == 9

9 // 16 == 0
0 % 16 == 0

To fix this, we need to calculate the remainder based on the original decimal_number and not touch it again.

def decToHex(decimal_number):
    hexStr = ''
    remainder = decimal_number % 16
    while decimal_number != 0 :
        decimal_number //= 16

        if decimal_number > 0:
            hexStr += getHexChar(decimal_number)
            hexStr += getHexChar(remainder)

    return hexStr 
That gives us the correct value. Try it out for some other values. Now, here's a bigger problem. What's the hex of 15,232? The function only works on numbers up to 16^2 (256).

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  morse code assignment raymond2688 11 580 Jul-29-2019, 07:43 PM
Last Post: raymond2688
  Write pseudo code for a class assignment Scrimshot 3 470 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Need help with lists to continue my assignment code tinabina22 9 4,269 Oct-12-2016, 12:20 AM
Last Post: Yoriz
  [split] Need help with lists to continue my assignment code cylandur 7 3,756 Oct-11-2016, 03:11 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)