mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
3
91_Train/python/README.md
Normal file
3
91_Train/python/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Python](https://www.python.org/about/)
|
||||
44
91_Train/python/train.py
Normal file
44
91_Train/python/train.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
# TRAIN
|
||||
#
|
||||
# Converted from BASIC to Python by Trevor Hobson
|
||||
|
||||
import random
|
||||
|
||||
|
||||
def play_game():
|
||||
"""Play one round of the game"""
|
||||
car_speed = random.randint(40, 65)
|
||||
time_difference = random.randint(5, 20)
|
||||
train_speed = random.randint(20, 39)
|
||||
print("\nA car travelling", car_speed, "MPH can make a certain trip in")
|
||||
print(time_difference, "hours less than a train travelling at", train_speed, "MPH")
|
||||
time_answer = 0
|
||||
while time_answer == 0:
|
||||
try:
|
||||
time_answer = float(input("How long does the trip take by car "))
|
||||
except ValueError:
|
||||
print("Please enter a number.")
|
||||
car_time = time_difference*train_speed/(car_speed-train_speed)
|
||||
error_percent = int(abs((car_time-time_answer)*100/time_answer)+.5)
|
||||
if error_percent > 5:
|
||||
print("Sorry. You were off by", error_percent, "percent.")
|
||||
print("Correct answer is", round(car_time, 6), "hours")
|
||||
else:
|
||||
print("Good! Answer within", error_percent, "percent.")
|
||||
|
||||
|
||||
def main():
|
||||
print(" " * 33 + "TRAIN")
|
||||
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
||||
print("Time - speed distance exercise")
|
||||
|
||||
keep_playing = True
|
||||
while keep_playing:
|
||||
play_game()
|
||||
keep_playing = input(
|
||||
"\nAnother problem (yes or no) ").lower().startswith("y")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user