Help for a client

Good morning,

I work for a Federal Defender office. We represent clients who have been charged with a federal crime and can’t afford to defend themselves. We have a client serving a life sentence who is learning to code in Python. It is really his passion and the only thing that keeps him sane at times. I was wondering if someone could paste his code into Python and let me know if there are any errors? Python is beyond my abilities…I am just a system admin. Thank you in advance. It is for a good cause.

… py3
with open (“rockpapscis.txt” “w”) as f:
import random
opt_set =4"Rock", “Paper”, “Scissors,.
opt_list = [“Rock”, “Paper”, “Scissors”]
pl_dic = ?Rock, Scissors”: “Rock Beats Scissors”, “Paper, Rock”: “Paper
Beats Rock”, “Scissors, Paper”: “Scissors Beat Paper”
cpdic =.t"Scissors Rock": “Rock Beats Scissors”, “Rock, Paper”: “Paper
Beats Rock”, "Papleir, Scissors: “Scissors Beat Paper”).
texts = ["Player Wine, “Computer Wins”, “Draw”, “Game”]
num = 1
plwins = 0
cpwins = 0
draws = 0
choices =
pl_choice = " "
while num < 6:
print(text[3] + + num)
inner_loop = True.
while inner_loop 7= True:
pl_choice = input(“Enter your selection: Rock, Paper or Scissors”)
if pl_choice in, ?pt_set == False:
print(“Incorrect entry. Try again.”)
else:
inner_loop = False
cp_choice = randomichoice(opt_list)
choices.extendapLchoice, cp_choice])
print(“Player has Chosen + " " + pl_choice + 1, + tt + “Computer has
chosen” + " " + Cp_choice + “.”)
for pl_key in pl_dic.keys():
if choices !=
for cp_key initp_dic.keys():
if choices == cp_key:
win = cp_dlc[choices]
print(win)
print(texts[1])
cp_wins += 1
1
num += 1
elif choices -= pl_key:
win = pldic[choices]
print(win)
print(texts[0])
pl_wins += 1
num ±= 1
else:
print(texts[2])
draws += 1
num += 1
print(“Game Over”)
print(“Player has” 4- /7 pl_wins + " " wins.)
print(“Computer has” + If It + cpwins + " " + wins.)
print(“T It It here are” + draws + “draws”)
if plwins > cR_wins
print(texts[0) + pl_wins + cR_wins + draws)
if pl_wins < cp_wins :
print(texts[1] + + plwins + draws)
else:
print(“Series was a draw”)
with open (“beersong.txt” “w”) as f:
bn = 99
nn = 98
v = [ bottles”, “of beer on the wall.”, “of beer. Take one down. Pass it
around.”, “bottle” “There’s no more bottles of beer on the wall.”]
while bn > 2: I
print(bn + " " + v[O] + " " + v[1] + " " + bn + " " + v[0]+ " " +
v[2] + u " + nn + " " + v[O] + " " + v[1])
bn -= 1
nn -= 1
if bn == 2:
print(bn + " it + v[0] + 31 II + v1.11.1
It II

  • bn + r II II + v[0] + " n v[2] +
    II It + nn + " " + v[3] + " II + v[1])
    bn -= 1
    nn -= 1
    if bn == 1:
    print(bn + " "t, v[3] + U U + v[1] + " " + bn + " " + v[3] +
    v[2] + " " + v[4])
    II II
    C 3
    with open (“conversion.text” “w”) as f:
    def temp_conversion (degrees, scale):
    if scale == Fahrenheit:
    Celsius = (degrees - 32) * 5/9
    Kelvin = Celsius + 273.15
    print(Celsius + “C”)
    print(Kelvin + “K”)
    if scale == Celsius:
    Fahrenheit = (degrees + 32) * 9/5
    Kelvin = degrees + 273.15
    print(Fahrenheit + “°F”)
    print(Kelvin + “K”)

There are many, but the majority are because your copy/paste has lost
all the indenting, and indenting is critical in Python. In fact, on
examining the code, is it possible it came from an OCR of a piece of
paper? There are many odd syntax errors which look like
mistranscription.

I’ll run my eye over it and have a guess about the indenting, but a
correctly indented version would be more helpful.

Assorted comments inline below.

with open ("rockpapscis.txt" "w") as f:

Your client is opening this file and storing a reference to the open
file as the variable “f”. But they never use that variable and therefore
never read from the file. The same for the other "open"s in the code.

import random

Normally all import statements go at the top of the programme where they
can be seen easily, before any of the code which does stuff.

opt_set =4"Rock", "Paper", "Scissors,.

I’m guessing this should be a set of strings: {"Rock","Paper","Scissors"}.

opt_list = ["Rock", "Paper", "Scissors"]
pl_dic = ?Rock, Scissors": "Rock Beats Scissors", "Paper, Rock": "Paper
Beats Rock", "Scissors, Paper": "Scissors Beat Paper"

And I’m guessing this should be a dict comprehension:

pl_dic = {
    "Rock, Scissors": "Rock Beats Scissors",
    "Paper, Rock": "Paper Beats Rock",
    "Scissors, Paper": "Scissors Beat Paper",
}

Skimming onwards…

cpdic =.t"Scissors Rock": "Rock Beats Scissors", "Rock, Paper": "Paper
Beats Rock", "Papleir, Scissors: "Scissors Beat Paper").
texts = ["Player Wine, "Computer Wins", "Draw", "Game"]
num = 1
plwins = 0
cpwins = 0
draws = 0
choices = []
pl_choice = " "
while num < 6:
print(text[3] + + num)
inner_loop = True.
while inner_loop 7= True:

I think this is meant to be:

while inner_loop == True:

You do not need to compare Booleans with Booleans like this - you can
use them directly, eg:

while inner_loop:

Next code:

pl_choice = input("Enter your selection: Rock, Paper or Scissors")
if pl_choice in, ?pt_set == False:
print("Incorrect entry. Try again.")

Again, you don’t need to compare to False. You can just go:

if not( pl_choice in pt_set ):

or better still:

if pl_choice not in pt_set:

using Python’s handy “not in” syntax.

else:
inner_loop = False

It looks like the inner loop is meant to be around the prompt, which is
reasonable. Normally indented that might look like:

while inner_loop:
    if pl_choice in, ?pt_set == False:
        print("Incorrect entry. Try again.")
    else:
        inner_loop = False

Rather than maintaining a separate variable for the loop control, your
client could go with the objective of the loop directly, that pl_choice
is a valid choice:

pl_choice = None    # definitely invalid
while pl_choice not in pt_set:
    if pl_choice is not None:
        print("Incorrect entry. Try again.")
    pl_choice = input("Enter your selection: Rock, Paper or Scissors")
# Here, after the loop, we know pl_choice is valid and we can
# proceed. We know it because the loop condition itself tests it.

Skipping again:

for pl_key in pl_dic.keys():

This can be written:

for pl_key in pl_dic:

After this things get more complicated, too complicated to review in
this form. It looks like you’ve got three distinct small programmes all
bunched together.

If you can get proper machine readable text to/from your client it would
be a great help. Can your client even run this stuff he is? Can he
email?

Cheers,
Cameron Simpson cs@cskk.id.au