Skip to content

Instantly share code, notes, and snippets.

@gsinclair
Created February 22, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsinclair/2fd37c8194ed248c7e2917382c465486 to your computer and use it in GitHub Desktop.
Save gsinclair/2fd37c8194ed248c7e2917382c465486 to your computer and use it in GitHub Desktop.
Work-in-progress solution to pre-release problem (Feb 2020)
def name(sack_type):
if sack_type == 'S': return 'sand'
elif sack_type == 'G': return 'gravel'
elif sack_type == 'C': return 'cement'
def input_sack_details(n):
global NREJECTS
while True:
prompt = "Sack " + str(n) + "> "
user_input = input(prompt)
sack_type, weight = user_input.split()
weight = float(weight)
if sack_type not in ['S', 'G', 'C']:
print(" *** Invalid sack type")
NREJECTS += 1
continue
if sack_type == 'S' or sack_type == 'G':
if weight <= 49.9 or weight >= 50.1:
print(" * Rejected: weight not in range")
NREJECTS += 1
continue
if sack_type == 'C':
if weight <= 24.9 or weight >= 25.1:
print(" * Rejected: weight not in range")
NREJECTS += 1
continue
# If we get this far, the input was valid.
TYPES.append(sack_type)
WEIGHTS.append(weight)
print(" * Accepted: %s %f kg" % (name(sack_type), weight))
return
def print_summary():
pass
# ------------------------------------------------------------------------------
# Key variables.
N = 0
TYPES = []
WEIGHTS =[]
NREJECTS = 0
# Ask user for number of sacks.
N = int(input("Number of sacks> "))
# Loop, accepting user input and updating variables, then print summary.
nsacks = 0 # The number accepted so far. Loop until we have enough.
while nsacks < N:
input_sack_details(nsacks+1)
nsacks += 1
print_summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment