Search In
• More options...
Find results that contain...
Find results in...

# Python

I was wondering if someone could explain this python coding to me in detail, Im having a hard time understanding it
What would be the simplest way, to get to the final solution, and why ?
So I can understand the thinking pattern of someone

##### Share on other sites

They want you to use variables, input(), while True loop, do string formatting with a print statement, do math operations, etc etc etc...

Here is a documented sample that does most of these things.  Pseudo code is mostly non-existant, but comments are generous.  The code does not handle improper input, but it does not seem your examples require this.  It will not be the exact program you are being asked for.

```"""
Program = Total income calculator
Author =
Purpose = Calculate income based wage and hours worked

Structure...

Gather user input for wage, normal hours, and OT hours
Calculate normal pay, OT pay, and total pay
Display normal pay, OT pay, and total pay
"""

#define static variable for OT rate
statOTRate = 1.5

#take input for hourly wage as float, store in hourlyWage
hourlyWage = float(input("What is your hourly wage?"))
#take input for normal time hours as float, store in standardHours
standardHours = float(input("How many normal hours did you work?"))
#take input for overtime hours as float, store in overtimeHours
overtimeHours = float(input("How many overtime hours did you work?"))

#calculate normal time pay, store in normalPay
normalPay = hourlyWage * standardHours
#calculate overtime pay, store in overtimePay
overtimePay = hourlyWage * overtimeHours * statOTRate
#calculate total pay, store in totalPay
totalPay = normalPay + overtimePay

#display results of calculations in an easy to understand format, rounded to the nearest cent
print ("Your normal time pay is...")
print ("\${:.2f}\n".format(round(normalPay,2)))
print ("\${:.2f}\n".format(round(overtimePay,2)))
print ("\${:.2f}".format(round(totalPay,2)))
```

##### Share on other sites
4 minutes ago, KarathKasun said:

They want you to use variables, input(), while True loop, do string formatting with a print statement, do math operations, etc etc etc...

Here is a documented sample that does these things.  Pseudo code is mostly non-existant, but comments are generous.  The code does not handle improper input, but it does not seem your examples require this.  It will not be the exact program you are being asked for.

```
"""
Program = Total income calculator
Author =
Purpose = Calculate income based wage and hours worked

Structure...

Gather user input for wage, normal hours, and OT hours
Calculate normal pay, OT pay, and total pay
Display normal pay, OT pay, and total pay
"""

#define static variable for OT rate
statOTRate = 1.5

#take input for hourly wage as float, store in hourlyWage
hourlyWage = float(input("What is your hourly wage?"))
#take input for normal time hours as float, store in standardHours
standardHours = float(input("How many normal hours did you work?"))
#take input for overtime hours as float, store in overtimeHours
overtimeHours = float(input("How many overtime hours did you work?"))

#calculate normal time pay, store in normalPay
normalPay = hourlyWage * standardHours
#calculate overtime pay, store in overtimePay
overtimePay = hourlyWage * overtimeHours * statOTRate
#calculate total pay, store in totalPay
totalPay = normalPay + overtimePay

#display results of calculations in an easy to understand format, rounded to the nearest cent
print ("Your normal time pay is...")
print ("\${:.2f}".format(round(normalPay,2)))
print ("\n")
print ("\${:.2f}".format(round(overtimePay,2)))
print ("\n")
print ("\${:.2f}".format(round(totalPay,2)))
```

Ahh okay, Im getting more of an understanding since of the pseudo code you sent me
Its a lot more split up and to the point then the way he was describing in class
Still is a little bit blurry in some areas

##### Share on other sites

Here is a more complete example with better pseudocode.  Hadnt gotten into functions at the time, so menus are a bit... long winded.  Comments only cover sections of the code where things are not largely the same.

```'''

Convert numbers into binary, octal, or hexidecimal and back while logging output.
**may contain easter eggs, be the first to get ye flask!**
Thy Dungeonman! references belong to homestarrunner.com

Variables...
getUserInput1
getUserInput2
getInputNumber
outputNumber
userOutFile
outFile

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

DISPLAY "Enter a name for the log file"

Get user input and store in userOutFile

REPEAT UNTIL BREAK

DISPLAY
"
Enter 2 to convert from binary
Enter 8 to convert from octal
Enter 10 to convert from decimal
Enter 16 to convert from hexidecimal
Enter Q to quit
"

Get user input and store in getUserInput1

IF getUserInput = 2

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 8 to convert to octal
Enter 10 to convert to decimal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Oct outputNumber"
DISPLAY "Bin getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Dec outputNumber"
DISPLAY "Bin getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Hex outputNumber"
DISPLAY "Bin getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 8

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 2 to convert to binary
Enter 10 to convert to decimal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Bin outputNumber"
DISPLAY "Oct getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Dec outputNumber"
DISPLAY "Oct getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Hex outputNumber"
DISPLAY "Oct getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 10

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

DISPLAY
"
Enter 2 to convert to binary
Enter 8 to convert to octal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Bin outputNumber"
DISPLAY "Dec getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Oct outputNumber"
DISPLAY "Dec getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Hex outputNumber"
DISPLAY "Dec getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 16

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 2 to convert to binary
Enter 8 to convert to octal
Enter 10 to convert to decimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Bin outputNumber"
DISPLAY "Hex getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Oct outputNumber"
DISPLAY "Hex getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Dec outputNumber"
DISPLAY "Hex getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

Display the following message
"Invalid input, try again"

IF getUserInput = Q or q

DISPLAY "Exiting program..."

BREAK

'''

#1 import os for disk function, initialize variables, get input for userOutFile variable
import os
strYeFlask="\nYe cannot get the FLASK.  It is firmly\nbolted to a wall which is bolted to the\nrest of the dungeon which is probably\nbolted to a castle.  Never you mind."
strYeFlask2="\nOkay, okay.  You unbolt yon FLASK and hold\nit aloft.  A great shaking begins.  The\ndungeon ceiling collapses down upon you,\ncrushing you in twain.  Apparently, this was\na load-bearing FLASK.\nYour score was: -998\nGAME OVER"
userOutFile = str(input("Enter a name for the log file: "))

#2 main loop
while True:

print("\nEnter 2 to convert from binary\nEnter 8 to convert from octal\nEnter 10 to convert from decimal\nEnter 16 to convert from hexidecimal\nEnter Q to quit\n")
getUserInput1 = str(input(":"))

if getUserInput1 == str("2"):

#4 get input for conversion from binary
getInputNumber = str(input(":"))

#5 loop for "convert from binary menu"
while True:

#6 display menu, get input for menu, if statements to handle input
print("\nEnter 8 to convert to octal\nEnter 10 to convert to decimal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("8"):

#7 convert getInputNumber to an integer, convert that to octal,
#remove 0o from oct() function output, create outString, open the log file
#concatenate outString with a newline character, write that to outFile/log
#display outString concatenated with newline and "Convert to another base?" message
#close outFile
outNumber = str(oct(int(getInputNumber,2))).replace("0o","")
outString = "Binary " + getInputNumber + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

#see number 7, small changes
outNumber = str(int(getInputNumber,2))
outString = "Binary " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

#see #7, small changes
outNumber = str(hex(int(getInputNumber,2))).replace("0x","")
outString = "Binary " + getInputNumber + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

#Code beyond this point is largely the same as above with small changes to change to different bases
#End of the program does contain an easter egg, #10

elif getUserInput1 == str(8):

getInputNumber = str(input(":"))

while True:

print("\nEnter 2 to convert to binary\nEnter 10 to convert to decimal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(int(getInputNumber,8))).replace("0b","")
outString = "Octal " + getInputNumber + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

outNumber = str(int(getInputNumber,8))
outString = "Octal " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

outNumber = str(hex(int(getInputNumber,8))).replace("0x","")
outString = "Octal " + getInputNumber + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1 == str(10):

getInputNumber = int(input(":"))

while True:

print("\nEnter 2 to convert to binary\nEnter 8 to convert to octal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(getInputNumber)).replace("0b","")
outString = "Decimal " + str(getInputNumber) + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("8"):

outNumber = str(oct(getInputNumber)).replace("0o","")
outString = "Decimal " + str(getInputNumber) + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

outNumber = str(hex(getInputNumber)).replace("0x","")
outString = "Decimal " + str(getInputNumber) + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1 == str(16):

getInputNumber = str(input(":"))

while True:
print("\nEnter 2 to convert to binary\nEnter 8 to convert to octal\nEnter 10 to convert to decimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(int(getInputNumber,16))).replace("0b","")
outString = "Hexidecimal " + getInputNumber + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("8"):

outNumber = str(oct(int(getInputNumber,16))).replace("0o","")
outString = "Hexidecimal " + getInputNumber + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

outNumber = str(int(getInputNumber,16))
outString = "Hexidecimal " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1.lower() == str("q"):

print("\nExiting program...")

break

#10 BONUS/EASTER EGG
elif getUserInput1.lower() == str("get ye flask"):

outFile = open(userOutFile+".txt","a")
outFile.close()
break

else:
outFile = open(userOutFile+".txt","a")
outFile.close()

else:
print("INVALID INPUT MORTAL!")
```

##### Share on other sites
4 minutes ago, KarathKasun said:

Here is a more complete example with better pseudocode.  Hadnt gotten into functions at the time, so menus are a bit... long winded.  Comments only cover sections of the code where things are not largely the same.

```
'''

Convert numbers into binary, octal, or hexidecimal and back while logging output.
**may contain easter eggs, be the first to get ye flask!**
Thy Dungeonman! references belong to homestarrunner.com

Variables...
getUserInput1
getUserInput2
getInputNumber
outputNumber
userOutFile
outFile

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

DISPLAY "Enter a name for the log file"

Get user input and store in userOutFile

REPEAT UNTIL BREAK

DISPLAY
"
Enter 2 to convert from binary
Enter 8 to convert from octal
Enter 10 to convert from decimal
Enter 16 to convert from hexidecimal
Enter Q to quit
"

Get user input and store in getUserInput1

IF getUserInput = 2

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 8 to convert to octal
Enter 10 to convert to decimal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Oct outputNumber"
DISPLAY "Bin getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Dec outputNumber"
DISPLAY "Bin getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Hex outputNumber"
DISPLAY "Bin getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 8

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 2 to convert to binary
Enter 10 to convert to decimal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Bin outputNumber"
DISPLAY "Oct getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Dec outputNumber"
DISPLAY "Oct getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Hex outputNumber"
DISPLAY "Oct getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 10

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

DISPLAY
"
Enter 2 to convert to binary
Enter 8 to convert to octal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Bin outputNumber"
DISPLAY "Dec getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Oct outputNumber"
DISPLAY "Dec getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Hex outputNumber"
DISPLAY "Dec getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 16

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 2 to convert to binary
Enter 8 to convert to octal
Enter 10 to convert to decimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Bin outputNumber"
DISPLAY "Hex getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Oct outputNumber"
DISPLAY "Hex getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Dec outputNumber"
DISPLAY "Hex getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

Display the following message
"Invalid input, try again"

IF getUserInput = Q or q

DISPLAY "Exiting program..."

BREAK

'''

#1 import os for disk function, initialize variables, get input for userOutFile variable
import os
strYeFlask="\nYe cannot get the FLASK.  It is firmly\nbolted to a wall which is bolted to the\nrest of the dungeon which is probably\nbolted to a castle.  Never you mind."
strYeFlask2="\nOkay, okay.  You unbolt yon FLASK and hold\nit aloft.  A great shaking begins.  The\ndungeon ceiling collapses down upon you,\ncrushing you in twain.  Apparently, this was\na load-bearing FLASK.\nYour score was: -998\nGAME OVER"
userOutFile = str(input("Enter a name for the log file: "))

#2 main loop
while True:

print("\nEnter 2 to convert from binary\nEnter 8 to convert from octal\nEnter 10 to convert from decimal\nEnter 16 to convert from hexidecimal\nEnter Q to quit\n")
getUserInput1 = str(input(":"))

if getUserInput1 == str("2"):

#4 get input for conversion from binary
getInputNumber = str(input(":"))

#5 loop for "convert from binary menu"
while True:

#6 display menu, get input for menu, if statements to handle input
print("\nEnter 8 to convert to octal\nEnter 10 to convert to decimal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("8"):

#7 convert getInputNumber to an integer, convert that to octal,
#remove 0o from oct() function output, create outString, open the log file
#concatenate outString with a newline character, write that to outFile/log
#display outString concatenated with newline and "Convert to another base?" message
#close outFile
outNumber = str(oct(int(getInputNumber,2))).replace("0o","")
outString = "Binary " + getInputNumber + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

#see number 7, small changes
outNumber = str(int(getInputNumber,2))
outString = "Binary " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

#see #7, small changes
outNumber = str(hex(int(getInputNumber,2))).replace("0x","")
outString = "Binary " + getInputNumber + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

#Code beyond this point is largely the same as above with small changes to change to different bases
#End of the program does contain an easter egg, #10

elif getUserInput1 == str(8):

getInputNumber = str(input(":"))

while True:

print("\nEnter 2 to convert to binary\nEnter 10 to convert to decimal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(int(getInputNumber,8))).replace("0b","")
outString = "Octal " + getInputNumber + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

outNumber = str(int(getInputNumber,8))
outString = "Octal " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

outNumber = str(hex(int(getInputNumber,8))).replace("0x","")
outString = "Octal " + getInputNumber + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1 == str(10):

getInputNumber = int(input(":"))

while True:

print("\nEnter 2 to convert to binary\nEnter 8 to convert to octal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(getInputNumber)).replace("0b","")
outString = "Decimal " + str(getInputNumber) + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("8"):

outNumber = str(oct(getInputNumber)).replace("0o","")
outString = "Decimal " + str(getInputNumber) + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

outNumber = str(hex(getInputNumber)).replace("0x","")
outString = "Decimal " + str(getInputNumber) + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1 == str(16):

getInputNumber = str(input(":"))

while True:
print("\nEnter 2 to convert to binary\nEnter 8 to convert to octal\nEnter 10 to convert to decimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(int(getInputNumber,16))).replace("0b","")
outString = "Hexidecimal " + getInputNumber + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("8"):

outNumber = str(oct(int(getInputNumber,16))).replace("0o","")
outString = "Hexidecimal " + getInputNumber + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

outNumber = str(int(getInputNumber,16))
outString = "Hexidecimal " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1.lower() == str("q"):

print("\nExiting program...")

break

#10 BONUS/EASTER EGG
elif getUserInput1.lower() == str("get ye flask"):

outFile = open(userOutFile+".txt","a")
outFile.close()
break

else:
outFile = open(userOutFile+".txt","a")
outFile.close()

else:
print("INVALID INPUT MORTAL!")
```

Thank you so much
This is helping me a lot more than before

##### Share on other sites
16 minutes ago, KarathKasun said:

They want you to use variables, input(), while True loop, do string formatting with a print statement, do math operations, etc etc etc...

Here is a documented sample that does most of these things.  Pseudo code is mostly non-existant, but comments are generous.  The code does not handle improper input, but it does not seem your examples require this.  It will not be the exact program you are being asked for.

```
"""
Program = Total income calculator
Author =
Purpose = Calculate income based wage and hours worked

Structure...

Gather user input for wage, normal hours, and OT hours
Calculate normal pay, OT pay, and total pay
Display normal pay, OT pay, and total pay
"""

#define static variable for OT rate
statOTRate = 1.5

#take input for hourly wage as float, store in hourlyWage
hourlyWage = float(input("What is your hourly wage?"))
#take input for normal time hours as float, store in standardHours
standardHours = float(input("How many normal hours did you work?"))
#take input for overtime hours as float, store in overtimeHours
overtimeHours = float(input("How many overtime hours did you work?"))

#calculate normal time pay, store in normalPay
normalPay = hourlyWage * standardHours
#calculate overtime pay, store in overtimePay
overtimePay = hourlyWage * overtimeHours * statOTRate
#calculate total pay, store in totalPay
totalPay = normalPay + overtimePay

#display results of calculations in an easy to understand format, rounded to the nearest cent
print ("Your normal time pay is...")
print ("\${:.2f}\n".format(round(normalPay,2)))
print ("\${:.2f}\n".format(round(overtimePay,2)))
print ("\${:.2f}".format(round(totalPay,2)))
```

Im stuggling myself I just got into coding (with python and others) Are you able to give me a example of the veriables in action with a if statement to create a outcome with a specific ammount.

##### Share on other sites
4 minutes ago, Difice said:

Im stuggling myself I just got into coding (with python and others) Are you able to give me a example of the veriables in action with a if statement to create a outcome with a specific ammount.

Check my 2nd post.  Code in that program handles variables and text menus with if/elif/else statements.

The code at the top is industry standardish pseudocode.  Real python starts at about halfway down after the comment ending ''' and the #1 comment.

##### Share on other sites

If you want the 2nd example, the base converter, to make more sense with the pseudocode... Copy paste the pseudo code into notepad and line it up with the start of the python code, side by side.  Then you can see what python code relates to what the pseudo code is saying to do.

The first "REPEAT UNTIL BREAK" corresponds to the first "while True:"

##### Share on other sites
On 10/24/2018 at 5:17 AM, KarathKasun said:

Here is a more complete example with better pseudocode.  Hadnt gotten into functions at the time, so menus are a bit... long winded.  Comments only cover sections of the code where things are not largely the same.

```
'''

Convert numbers into binary, octal, or hexidecimal and back while logging output.
**may contain easter eggs, be the first to get ye flask!**
Thy Dungeonman! references belong to homestarrunner.com

Variables...
getUserInput1
getUserInput2
getInputNumber
outputNumber
userOutFile
outFile

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

DISPLAY "Enter a name for the log file"

Get user input and store in userOutFile

REPEAT UNTIL BREAK

DISPLAY
"
Enter 2 to convert from binary
Enter 8 to convert from octal
Enter 10 to convert from decimal
Enter 16 to convert from hexidecimal
Enter Q to quit
"

Get user input and store in getUserInput1

IF getUserInput = 2

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 8 to convert to octal
Enter 10 to convert to decimal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Oct outputNumber"
DISPLAY "Bin getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Dec outputNumber"
DISPLAY "Bin getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Bin getInputNumber = Hex outputNumber"
DISPLAY "Bin getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 8

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 2 to convert to binary
Enter 10 to convert to decimal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Bin outputNumber"
DISPLAY "Oct getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Dec outputNumber"
DISPLAY "Oct getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Oct getInputNumber = Hex outputNumber"
DISPLAY "Oct getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 10

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

DISPLAY
"
Enter 2 to convert to binary
Enter 8 to convert to octal
Enter 16 to convert to hexidecimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Bin outputNumber"
DISPLAY "Dec getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Oct outputNumber"
DISPLAY "Dec getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 16

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Dec getInputNumber = Hex outputNumber"
DISPLAY "Dec getInputNumber = Hex outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

DISPLAY "Invalid input, try again"

IF getUserInput = 16

REPEAT UNTIL BREAK

Get user input and store in getInputNumber

"
Enter 2 to convert to binary
Enter 8 to convert to octal
Enter 10 to convert to decimal
"

Get user input and store in getUserInput2

IF getUserInput2 = 2

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Bin outputNumber"
DISPLAY "Hex getInputNumber = Bin outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 8

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Oct outputNumber"
DISPLAY "Hex getInputNumber = Oct outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = 10

OPEN userOutFile as outFile for appending
Convert getInputNumber to octal, storing to outputNumber variable
Write to outFile "Hex getInputNumber = Dec outputNumber"
DISPLAY "Hex getInputNumber = Dec outputNumber"
CLOSE outFile

ELSE IF getUserInput2 = Q or q

BREAK

ELSE

Display the following message
"Invalid input, try again"

IF getUserInput = Q or q

DISPLAY "Exiting program..."

BREAK

'''

#1 import os for disk function, initialize variables, get input for userOutFile variable
import os
strYeFlask="\nYe cannot get the FLASK.  It is firmly\nbolted to a wall which is bolted to the\nrest of the dungeon which is probably\nbolted to a castle.  Never you mind."
strYeFlask2="\nOkay, okay.  You unbolt yon FLASK and hold\nit aloft.  A great shaking begins.  The\ndungeon ceiling collapses down upon you,\ncrushing you in twain.  Apparently, this was\na load-bearing FLASK.\nYour score was: -998\nGAME OVER"
userOutFile = str(input("Enter a name for the log file: "))

#2 main loop
while True:

print("\nEnter 2 to convert from binary\nEnter 8 to convert from octal\nEnter 10 to convert from decimal\nEnter 16 to convert from hexidecimal\nEnter Q to quit\n")
getUserInput1 = str(input(":"))

if getUserInput1 == str("2"):

#4 get input for conversion from binary
getInputNumber = str(input(":"))

#5 loop for "convert from binary menu"
while True:

#6 display menu, get input for menu, if statements to handle input
print("\nEnter 8 to convert to octal\nEnter 10 to convert to decimal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("8"):

#7 convert getInputNumber to an integer, convert that to octal,
#remove 0o from oct() function output, create outString, open the log file
#concatenate outString with a newline character, write that to outFile/log
#display outString concatenated with newline and "Convert to another base?" message
#close outFile
outNumber = str(oct(int(getInputNumber,2))).replace("0o","")
outString = "Binary " + getInputNumber + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

#see number 7, small changes
outNumber = str(int(getInputNumber,2))
outString = "Binary " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

#see #7, small changes
outNumber = str(hex(int(getInputNumber,2))).replace("0x","")
outString = "Binary " + getInputNumber + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

#Code beyond this point is largely the same as above with small changes to change to different bases
#End of the program does contain an easter egg, #10

elif getUserInput1 == str(8):

getInputNumber = str(input(":"))

while True:

print("\nEnter 2 to convert to binary\nEnter 10 to convert to decimal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(int(getInputNumber,8))).replace("0b","")
outString = "Octal " + getInputNumber + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

outNumber = str(int(getInputNumber,8))
outString = "Octal " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

outNumber = str(hex(int(getInputNumber,8))).replace("0x","")
outString = "Octal " + getInputNumber + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1 == str(10):

getInputNumber = int(input(":"))

while True:

print("\nEnter 2 to convert to binary\nEnter 8 to convert to octal\nEnter 16 to convert to hexidecimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(getInputNumber)).replace("0b","")
outString = "Decimal " + str(getInputNumber) + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("8"):

outNumber = str(oct(getInputNumber)).replace("0o","")
outString = "Decimal " + str(getInputNumber) + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("16"):

outNumber = str(hex(getInputNumber)).replace("0x","")
outString = "Decimal " + str(getInputNumber) + " = Hexidecimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1 == str(16):

getInputNumber = str(input(":"))

while True:
print("\nEnter 2 to convert to binary\nEnter 8 to convert to octal\nEnter 10 to convert to decimal\nEnter Q to return to main menu\n")
getUserInput2 = str(input(":"))

if getUserInput2.lower() == str("2"):

outNumber = str(bin(int(getInputNumber,16))).replace("0b","")
outString = "Hexidecimal " + getInputNumber + " = Binary " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("8"):

outNumber = str(oct(int(getInputNumber,16))).replace("0o","")
outString = "Hexidecimal " + getInputNumber + " = Octal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("10"):

outNumber = str(int(getInputNumber,16))
outString = "Hexidecimal " + getInputNumber + " = Decimal " + outNumber
outFile = open(userOutFile+".txt","a")
outFile.write(outString + "\n")
print("\n" + outString + "\n\nConvert to another base?")
outFile.close()

elif getUserInput2.lower() == str("q"):

break

else:

print("Invalid input!")

elif getUserInput1.lower() == str("q"):

print("\nExiting program...")

break

#10 BONUS/EASTER EGG
elif getUserInput1.lower() == str("get ye flask"):

outFile = open(userOutFile+".txt","a")
outFile.close()
break

else:
outFile = open(userOutFile+".txt","a")
outFile.close()

else:
print("INVALID INPUT MORTAL!")
```

See how in the conversion to binary your basically repeating your self over and over?

They are a perfect example of when you should refactor that into a single function that you pass data too.

If you find yourself repeating code you're doing something wrong.

¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

##### Share on other sites
6 minutes ago, vorticalbox said:

See how in the conversion to binary your basically repeating your self over and over?

They are a perfect example of when you should refactor that into a single function that you pass data too.

If you find yourself repeating code you're doing something wrong.

I am aware of this, but the class had not moved into functions at the time so I used what was covered up to that point to make the program.

##### Share on other sites
1 minute ago, KarathKasun said:

I am aware of this, but the class had not moved into functions at the time so I used what was covered up to that point to make the program.

That's a slow moving class If I were a teacher after learning basic operations I would move on to functions.

No point letting people code wrong to then have to relearn the thought process.

¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

##### Share on other sites
Just now, vorticalbox said:

That's a slow moving class If I were a teacher after learning basic operations I would move on to functions.

No point letting people code wrong to then have to relearn the thought process.

It was for something like the third class meeting.