3 Replies - 5403 Views - Last Post: 01 October 2012 - 07:41 PM Rate Topic: -----

#1 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 01:57 AM

When I enter a decimal integer the program should display the corresponding hexadecimal value.

And this is what I got so far.....

decimal = eval(input("Enter an integer: "))
   
n = decimal

while n != 0:  
    if n % 16 == 10:
        hexadecimal = 'A'
    elif n % 16 == 11:
        hexadecimal = 'B'
    elif n % 16 == 12:
        hexadecimal = 'C'
    elif n % 16 == 13:
        hexidecimal = 'D'
    elif n % 16 == 14:
        hexadecimal  = 'E'
    elif n % 16 == 15:
        hexadecimal = 'F'
    else:
        hexadecimal = n % 16
    
    hexadecimal = hex(n)
    

print("The", str(n) + "its corresponding to", hexadecimal)



For example decimal of 15 will be F (hex)
29 will be 1D (hex)

Apparently, my code is not working.

can anyone help!

Is This A Good Question/Topic? 0
  • +

Replies To: decimal integer to its corresponding hexadecimal value

#2 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 05:10 AM

Im no expert but I did notice you are in an infinite while loop. Since your code does nothing to modify n, n will always not equal 0. Also, I noticed your using the hex() function anyway so... why not just take the string the user inputs, convert it to an int and then use the hex() functions?

decimal = input('Please type an integer: ')
hexadecimal = hex(int(decimal))

print(decimal + ' is equivlent to ' + hexadecimal)


If im missing something please let me know. Im just not sure why you want to go through all that extra code when you use hex() anyway.
Was This Post Helpful? 2
  • +
  • -

#3 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Re: decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 11:04 AM

View Postmidknight51, on 29 September 2012 - 05:10 AM, said:

Im no expert but I did notice you are in an infinite while loop. Since your code does nothing to modify n, n will always not equal 0. Also, I noticed your using the hex() function anyway so... why not just take the string the user inputs, convert it to an int and then use the hex() functions?

decimal = input('Please type an integer: ')
hexadecimal = hex(int(decimal))

print(decimal + ' is equivlent to ' + hexadecimal)


If im missing something please let me know. Im just not sure why you want to go through all that extra code when you use hex() anyway.



I have to use the loop, and I don't know how to use a loop to write a decimal to hexadecimal code.
Was This Post Helpful? 0
  • +
  • -

#4 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: decimal integer to its corresponding hexadecimal value

Posted 01 October 2012 - 07:41 PM

Sorry for the delay, I figured your problem had been solved since you didnt answer for a while.
So if you HAVE to use a loop and your code needs to turn a decimal number into hex then...

#Declaring all my global variables here
decimal = ''
hexadecimal = ''
answer = ''
playAgain = True #Assigned true at the begining so program will run.

#Beginning of Program
print('Welcome! Please enter a number you would like converted into hexadecimal: ')
decimal = input()

while playAgain = True:
    hexadecimal = hex(int(decimal)) #Assign the variable hexadecimal the value of the conversion of the integer decimal.
    print(decimal + ' is equivlent to ' + hexadecimal)
    print('Would you like to try again? (y/n)')
    answer = input().lower()

#Now that the program is finished, lets see if the user is done.
    
    if answer != 'y' or 'yes':
        playAgain = False #If the user enters anything other than 'y' or 'yes', playAgain turns to False. Ending the while loop.


Im at work so I have no way to test this code. But that's the idea.
Hope this helps!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1