(New to programming, only been at it a month) Can somebody help me with this?

  • Thread starter Thread starter Jack Mercer
  • Start date Start date
J

Jack Mercer

Guest
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
getHexChar(dec_digit)

#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.
print(decToHex(userInput))

--------------------------------------------------------------------------------

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.

Continue reading...
 
Back
Top