Python: Fix string concatenation

This commit is contained in:
Martin Thoma
2022-03-15 13:57:28 +01:00
parent b47df50b6f
commit 5374569063
8 changed files with 50 additions and 73 deletions

View File

@@ -68,9 +68,7 @@ def get_valid_guess():
if len(set(guess)) == 3: if len(set(guess)) == 3:
valid = True valid = True
else: else:
print( print("Oh, I forgot to tell you that the number I have in mind")
"Oh, I forgot to tell you that " + "the number I have in mind"
)
print("has no two digits the same.") print("has no two digits the same.")
else: else:
print("What?") print("What?")
@@ -147,7 +145,7 @@ while still_running:
guesses += 1 guesses += 1
if guesses > MAX_GUESSES: if guesses > MAX_GUESSES:
print("Oh well") print("Oh well")
print(f"That's {MAX_GUESSES} guesses. " + "My number was " + num_str) print(f"That's {MAX_GUESSES} guesses. My number was {num_str}")
guessing = False guessing = False
valid_response = False valid_response = False

View File

@@ -23,8 +23,9 @@ class Basketball:
print("Υou will be Dartmouth captain and playmaker.") print("Υou will be Dartmouth captain and playmaker.")
print("Call shots as follows:") print("Call shots as follows:")
print( print(
"1. Long (30ft.) Jump Shot; 2. Short (15 ft.) Jump Shot; " "1. Long (30ft.) Jump Shot; "
+ "3. Lay up; 4. Set Shot" "2. Short (15 ft.) Jump Shot; "
"3. Lay up; 4. Set Shot"
) )
print("Both teams will use the same defense. Call Defense as follows:") print("Both teams will use the same defense. Call Defense as follows:")
print("6. Press; 6.5 Man-to-Man; 7. Zone; 7.5 None.") print("6. Press; 6.5 Man-to-Man; 7. Zone; 7.5 None.")

View File

@@ -104,7 +104,7 @@ def init_enemy():
# Messages correspond to outposts remaining (3, 2, 1, 0) # Messages correspond to outposts remaining (3, 2, 1, 0)
PLAYER_PROGRESS_MESSAGES = ( PLAYER_PROGRESS_MESSAGES = (
"YOU GOT ME, I'M GOING FAST. BUT I'LL GET YOU WHEN\n" "MY TRANSISTO&S RECUP%RA*E!", "YOU GOT ME, I'M GOING FAST. BUT I'LL GET YOU WHEN\nMY TRANSISTO&S RECUP%RA*E!",
"THREE DOWN, ONE TO GO.\n\n", "THREE DOWN, ONE TO GO.\n\n",
"TWO DOWN, TWO TO GO.\n\n", "TWO DOWN, TWO TO GO.\n\n",
"ONE DOWN, THREE TO GO.\n\n", "ONE DOWN, THREE TO GO.\n\n",
@@ -112,7 +112,7 @@ PLAYER_PROGRESS_MESSAGES = (
ENEMY_PROGRESS_MESSAGES = ( ENEMY_PROGRESS_MESSAGES = (
"YOU'RE DEAD. YOUR LAST OUTPOST WAS AT {}. HA, HA, HA.\n" "BETTER LUCK NEXT TIME.", "YOU'RE DEAD. YOUR LAST OUTPOST WAS AT {}. HA, HA, HA.\nBETTER LUCK NEXT TIME.",
"YOU HAVE ONLY ONE OUTPOST LEFT.\n\n", "YOU HAVE ONLY ONE OUTPOST LEFT.\n\n",
"YOU HAVE ONLY TWO OUTPOSTS LEFT.\n\n", "YOU HAVE ONLY TWO OUTPOSTS LEFT.\n\n",
"YOU HAVE ONLY THREE OUTPOSTS LEFT.\n\n", "YOU HAVE ONLY THREE OUTPOSTS LEFT.\n\n",

View File

@@ -19,16 +19,11 @@ def print_lightning_bolt():
def print_solution(n): def print_solution(n):
print( print(f"\n{n} plus 3 gives {n + 3}. This Divided by 5 equals {(n + 3) / 5}")
"\n{} plus 3 gives {}. This Divided by 5 equals {}".format(
n, n + 3, (n + 3) / 5
)
)
print(f"This times 8 gives {((n + 3) / 5) * 8}. If we divide 5 and add 5.") print(f"This times 8 gives {((n + 3) / 5) * 8}. If we divide 5 and add 5.")
print( print(
"We get {}, which, minus 1 equals {}".format( f"We get {(((n + 3) / 5) * 8) / 5 + 5}, "
(((n + 3) / 5) * 8) / 5 + 5, ((((n + 3) / 5) * 8) / 5 + 5) - 1 f"which, minus 1 equals {((((n + 3) / 5) * 8) / 5 + 5) - 1}"
)
) )

View File

@@ -61,7 +61,7 @@ def print_summary_report(running_correct: int):
if running_correct > 10: if running_correct > 10:
print() print()
print("I GUESSED MORE THAN 1/3 OF YOUR NUMBERS.") print("I GUESSED MORE THAN 1/3 OF YOUR NUMBERS.")
print("I WIN." + "\u0007") print("I WIN.\u0007")
elif running_correct < 10: elif running_correct < 10:
print("I GUESSED LESS THAN 1/3 OF YOUR NUMBERS.") print("I GUESSED LESS THAN 1/3 OF YOUR NUMBERS.")
print("YOU BEAT ME. CONGRATULATIONS *****") print("YOU BEAT ME. CONGRATULATIONS *****")

View File

@@ -14,7 +14,6 @@ Supports Python version 3.8 or later.
from random import random from random import random
from typing import Final, FrozenSet, Optional, Tuple from typing import Final, FrozenSet, Optional, Tuple
######################################################################################## ########################################################################################
# Optional configs # Optional configs
######################################################################################## ########################################################################################
@@ -178,8 +177,8 @@ def get_move(current_loc: Optional[Tuple[int, int]]) -> Tuple[int, int]:
else: else:
prompt = ( prompt = (
"PLEASE READ THE DIRECTIONS AGAIN.\n" "PLEASE READ THE DIRECTIONS AGAIN.\n"
+ "YOU HAVE BEGUN ILLEGALLY.\n\n" "YOU HAVE BEGUN ILLEGALLY.\n\n"
+ "WHERE WOULD YOU LIKE TO START? " "WHERE WOULD YOU LIKE TO START? "
) )
else: else:
if ( if (

View File

@@ -1,26 +1,24 @@
###################################################################### """
# Stars
# Stars
# From: BASIC Computer Games (1978)
# From: BASIC Computer Games (1978) Edited by David H. Ahl
# Edited by David H. Ahl
# "In this game, the computer selects a random number from 1 to 100
# "In this game, the computer selects a random number from 1 to 100 (or any value you set [for MAX_NUM]). You try to guess the number
# (or any value you set [for MAX_NUM]). You try to guess the number and the computer gives you clues to tell you how close you're
# and the computer gives you clues to tell you how close you're getting. One star (*) means you're far away from the number; seven
# getting. One star (*) means you're far away from the number; seven stars (*******) means you're really close. You get 7 guesses.
# stars (*******) means you're really close. You get 7 guesses.
# "On the surface this game is very similar to GUESS; however, the
# "On the surface this game is very similar to GUESS; however, the guessing strategy is quite different. See if you can come up with
# guessing strategy is quite different. See if you can come up with one or more approaches to finding the mystery number.
# one or more approaches to finding the mystery number.
# "Bob Albrecht of People's Computer Company created this game."
# "Bob Albrecht of People's Computer Company created this game."
#
# Python port by Jeff Jetton, 2019
# Python port by Jeff Jetton, 2019 """
#
######################################################################
import random import random
@@ -31,7 +29,7 @@ MAX_GUESSES = 7
def print_instructions(): def print_instructions():
# "*** Instructions on how to play" """Instructions on how to play"""
print("I am thinking of a whole number from 1 to %d" % MAX_NUM) print("I am thinking of a whole number from 1 to %d" % MAX_NUM)
print("Try to guess my number. After you guess, I") print("Try to guess my number. After you guess, I")
print("will type one or more stars (*). The more") print("will type one or more stars (*). The more")
@@ -88,8 +86,8 @@ while still_playing:
if guess == secret_number: if guess == secret_number:
# "*** We have a winner" # "*** We have a winner"
player_has_won = True player_has_won = True
print("*************************" + "*************************!!!") print("**************************************************!!!")
print("You got it in %d guesses!!!" % guess_number) print(f"You got it in {guess_number} guesses!!!")
else: else:
print_stars(secret_number, guess) print_stars(secret_number, guess)
@@ -98,9 +96,7 @@ while still_playing:
# "*** Did not guess in [MAX_GUESS] guesses" # "*** Did not guess in [MAX_GUESS] guesses"
if not player_has_won: if not player_has_won:
print( print(f"\nSorry, that's {guess_number} guesses, number was {secret_number}")
"\nSorry, that's %d guesses, number was %d" % (guess_number, secret_number)
)
# Keep playing? # Keep playing?
response = input("\nPlay again? ") response = input("\nPlay again? ")

View File

@@ -117,9 +117,7 @@ def navigation():
if warp == 0: if warp == 0:
return return
if warp < 0 or warp > 8: if warp < 0 or warp > 8:
print( print(f" CHIEF ENGINEER SCOTT REPORTS 'THE ENGINES WON'T TAKE WARP {warp}!'")
" CHIEF ENGINEER SCOTT REPORTS 'THE ENGINES WON'T TAKE " f"WARP {warp}!'"
)
return return
n = round(warp * 8) n = round(warp * 8)
@@ -160,9 +158,7 @@ def navigation():
print(f"DAMAGE CONTROL REPORT: {devices[r1]} DAMAGED\n") print(f"DAMAGE CONTROL REPORT: {devices[r1]} DAMAGED\n")
else: else:
d[r1] += random.random() * 3 + 1 d[r1] += random.random() * 3 + 1
print( print(f"DAMAGE CONTROL REPORT: {devices[r1]} STATE OF REPAIR IMPROVED\n")
f"DAMAGE CONTROL REPORT: {devices[r1]} STATE OF " "REPAIR IMPROVED\n"
)
# begin moving starship # begin moving starship
insert_marker(int(s1), int(s2), " ") insert_marker(int(s1), int(s2), " ")
@@ -204,7 +200,7 @@ def navigation():
q2 = s2 = 7 q2 = s2 = 7
if hit_edge: if hit_edge:
print("LT. UHURA REPORTS MESSAGE FROM STARFLEET COMMAND:") print("LT. UHURA REPORTS MESSAGE FROM STARFLEET COMMAND:")
print(" 'PERMISSION TO ATTEMPT CROSSING OF GALACTIC " "PERIMETER") print(" 'PERMISSION TO ATTEMPT CROSSING OF GALACTIC PERIMETER")
print(" IS HEREBY *DENIED*. SHUT DOWN YOUR ENGINES.'") print(" IS HEREBY *DENIED*. SHUT DOWN YOUR ENGINES.'")
print("CHIEF ENGINEER SCOTT REPORTS 'WARP ENGINES SHUT DOWN") print("CHIEF ENGINEER SCOTT REPORTS 'WARP ENGINES SHUT DOWN")
print( print(
@@ -392,14 +388,10 @@ def phaser_control():
h = int((h1 / fnd(i)) * (random.random() + 2)) h = int((h1 / fnd(i)) * (random.random() + 2))
if h <= 0.15 * k[i][2]: if h <= 0.15 * k[i][2]:
print( print(f"SENSORS SHOW NO DAMAGE TO ENEMY AT {k[i][0] + 1} , {k[i][1] + 1}")
"SENSORS SHOW NO DAMAGE TO ENEMY AT " f"{k[i][0] + 1} , {k[i][1] + 1}"
)
else: else:
k[i][2] -= h k[i][2] -= h
print( print(f" {h} UNIT HIT ON KLINGON AT SECTOR {k[i][0] + 1} , {k[i][1] + 1}")
f" {h} UNIT HIT ON KLINGON AT SECTOR " f"{k[i][0] + 1} , {k[i][1] + 1}"
)
if k[i][2] <= 0: if k[i][2] <= 0:
print("*** KLINGON DESTROYED ***") print("*** KLINGON DESTROYED ***")
k3 -= 1 k3 -= 1
@@ -515,9 +507,7 @@ def klingons_fire():
h = int((k[i][2] / fnd(i)) * (random.random() + 2)) h = int((k[i][2] / fnd(i)) * (random.random() + 2))
s -= h s -= h
k[i][2] /= random.random() + 3 k[i][2] /= random.random() + 3
print( print(f" {h} UNIT HIT ON ENTERPRISE FROM SECTOR {k[i][0] + 1} , {k[i][1] + 1}")
f" {h} UNIT HIT ON ENTERPRISE FROM SECTOR " f"{k[i][0] + 1} , {k[i][1] + 1}"
)
if s <= 0: if s <= 0:
end_game(won=False, quit=False, enterprise_killed=True) end_game(won=False, quit=False, enterprise_killed=True)
return return
@@ -525,7 +515,7 @@ def klingons_fire():
if h >= 20 and random.random() < 0.60 and h / s > 0.02: if h >= 20 and random.random() < 0.60 and h / s > 0.02:
r1 = fnr() r1 = fnr()
d[r1] -= h / s + 0.5 * random.random() d[r1] -= h / s + 0.5 * random.random()
print(f"DAMAGE CONTROL REPORTS '{devices[r1]} DAMAGED " "BY THE HIT'") print(f"DAMAGE CONTROL REPORTS '{devices[r1]} DAMAGED BY THE HIT'")
def shield_control(): def shield_control():
@@ -585,7 +575,7 @@ def damage_control():
if d3 >= 1: if d3 >= 1:
d3 = 0.9 d3 = 0.9
print("\nTECHNICIANS STANDING BY TO EFFECT REPAIRS TO YOUR SHIP;") print("\nTECHNICIANS STANDING BY TO EFFECT REPAIRS TO YOUR SHIP;")
print("ESTIMATED TIME TO REPAIR: " f"{round(0.01 * int(100 * d3), 2)} STARDATES") print(f"ESTIMATED TIME TO REPAIR: {round(0.01 * int(100 * d3), 2)} STARDATES")
if input("WILL YOU AUTHORIZE THE REPAIR ORDER (Y/N)? ").upper().strip() != "Y": if input("WILL YOU AUTHORIZE THE REPAIR ORDER (Y/N)? ").upper().strip() != "Y":
return return
@@ -676,9 +666,7 @@ def computer():
) )
return return
print( print(f"FROM ENTERPRISE TO KLINGON BATTLE CRUISER{'S' if k3 > 1 else ''}")
"FROM ENTERPRISE TO KLINGON BATTLE " f"CRUISER{'S' if k3 > 1 else ''}"
)
for i in range(3): for i in range(3):
if k[i][2] > 0: if k[i][2] > 0:
@@ -697,7 +685,7 @@ def computer():
return return
elif com == 4: elif com == 4:
print("DIRECTION/DISTANCE CALCULATOR:") print("DIRECTION/DISTANCE CALCULATOR:")
print(f"YOU ARE AT QUADRANT {q1+1} , {q2+1} SECTOR " f"{s1+1} , {s2+1}") print(f"YOU ARE AT QUADRANT {q1+1} , {q2+1} SECTOR {s1+1} , {s2+1}")
print("PLEASE ENTER") print("PLEASE ENTER")
while True: while True:
coordinates = input(" INITIAL COORDINATES (X,Y)? ").split(",") coordinates = input(" INITIAL COORDINATES (X,Y)? ").split(",")
@@ -908,7 +896,7 @@ def end_game(won=False, quit=True, enterprise_killed=False):
if won: if won:
print("CONGRATULATIONS, CAPTAIN! THE LAST KLINGON BATTLE CRUISER") print("CONGRATULATIONS, CAPTAIN! THE LAST KLINGON BATTLE CRUISER")
print("MENACING THE FEDERATION HAS BEEN DESTROYED.\n") print("MENACING THE FEDERATION HAS BEEN DESTROYED.\n")
print("YOUR EFFICIENCY RATING IS " f"{round(1000 * (k7 / (t - t0))**2, 4)}\n\n") print(f"YOUR EFFICIENCY RATING IS {round(1000 * (k7 / (t - t0))**2, 4)}\n\n")
else: else:
if not quit: if not quit:
if enterprise_killed: if enterprise_killed:
@@ -926,7 +914,7 @@ def end_game(won=False, quit=True, enterprise_killed=False):
print("THE FEDERATION IS IN NEED OF A NEW STARSHIP COMMANDER") print("THE FEDERATION IS IN NEED OF A NEW STARSHIP COMMANDER")
print("FOR A SIMILAR MISSION -- IF THERE IS A VOLUNTEER,") print("FOR A SIMILAR MISSION -- IF THERE IS A VOLUNTEER,")
if input("LET HIM STEP FORWARD AND " "ENTER 'AYE'? ").upper().strip() != "AYE": if input("LET HIM STEP FORWARD AND ENTER 'AYE'? ").upper().strip() != "AYE":
exit() exit()
restart = True restart = True