Replies To: decimal integer to its corresponding hexadecimal value

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?

Re: decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 11:04 AM

midknight51, 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?

Re: decimal integer to its corresponding hexadecimal value

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!