Parse Basic Programming - basic

Receiving this error when trying to compile code on replit. I have already tried to declare two arrays, you need to use separate DIM statements for each array:
as 20 DIM W$(10)
30 DIM G$(26)
Error: ParseError: Parse error on line 20: Unexpected token DIM
Any suggestions on how to resolve this issue
10 REM Hangman game
20 REM Set up variables
30 DIM W$(10), G$(26)
40 LET C = 0
50 LET I = 0
60 LET J = 0
70 REM Set up word list
80 W$(1) = "HANGMAN"
90 W$(2) = "BASIC"
100 W$(3) = "COMPUTER"
110 W$(4) = "PROGRAM"
120 W$(5) = "VINTAGE"
130 REM Select a random word
140 LET R = INT(RND(1) * 5) + 1
150 LET W = W$(R)
160 REM Set up guess string
170 FOR I = 1 TO LEN(W)
180 G$(I) = "-"
190 NEXT I
200 REM Main game loop
210 DO
220 CLS
230 PRINT "Hangman"
240 PRINT
250 PRINT "Word: ";
260 FOR I = 1 TO LEN(W)
270 PRINT G$(I);
280 NEXT I
290 PRINT
300 PRINT "Guesses: ";
310 FOR I = 1 TO 26
320 IF G$(I) <> "-" THEN PRINT G$(I);
330 NEXT I
340 PRINT
350 INPUT "Enter a letter: ", L$
360 IF LEN(L$) > 1 THEN 400
370 IF L$ < "A" OR L$ > "Z" THEN 400
380 LET L = ASC(L$) - 64
390 GOTO 420
400 PRINT "Invalid input. Please enter a single letter."
410 GOTO 350
420 REM Check if letter is in word
430 LET F = 0
440 FOR I = 1 TO LEN(W)
450 IF MID$(W, I, 1) = L$ THEN G$(I) = L$: F = 1
460 NEXT I
470 IF F = 0 THEN C = C + 1
480 IF C = 6 THEN 600
490 REM Check for win
500 LET WN = 1
510 FOR I = 1 TO LEN(W)
520 IF G$(I) = "-" THEN WN = 0
530 NEXT I
540 IF WN THEN PRINT "You win!": GOTO 650
550 REM Check for loss
560 IF C = 6 THEN PRINT "You lose. The word was "; W: GOTO 650
570 LOOP
600 REM Draw hangman
610 PRINT " _____"
620 PRINT " | |"
630 IF C > 1 THEN PRINT " O |" ELSE PRINT " |"
640 IF C > 2 THEN PRINT "/|\ |" ELSE PRINT " |"
650 END
/////
I tried declaring separate arrays. I also tried running it on a vintage basic terminal, and received an error with line 150 "type mismatch"

Related

Why do I get a view error when enumerating a Dataframe

Why do I get a "view" error:
ndf = pd.DataFrame()
ndf['Signals'] = [1,1,1,1,1,0,0,0,0,0]
signals_diff = ndf.Signals.diff()
ndf['Revals'] = [101,102,105,104,105,106,107,108,109,109]
ndf['Entry'] = 0
for i,element in enumerate(signals_diff):
if (i==0):
ndf.iloc[i]['Entry'] = ndf.iloc[i]['Revals']
elif (element == 0):
ndf.iloc[i]['Entry'] = ndf.iloc[i - 1]['Entry']
else:
ndf.iloc[i]['Entry'] = ndf.iloc[i]['Revals']
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
ndf.iloc[i]['Entry'] = ndf.iloc[i]['Revals']
instead of iloc use loc:
ndf = pd.DataFrame()
ndf['Signals'] = [1,1,1,1,1,0,0,0,0,0]
signals_diff = ndf.Signals.diff()
ndf['Revals'] = [101,102,105,104,105,106,107,108,109,109]
ndf['Entry'] = 0
for i,element in enumerate(signals_diff):
if (i==0):
ndf.loc[i,'Entry'] = ndf.loc[i,'Revals']
elif (element == 0):
ndf.loc[i,'Entry'] = ndf.loc[i - 1,'Entry']
else:
ndf.loc[i,'Entry'] = ndf.loc[i,'Revals']
This will solve the problem but when assigning, the index should be same. So because of the index thing you might not be able to get the expected result.
Do not chain indexes like ndf.iloc[i]['Entry'] when trying to assign something. See why does that not work.
That said, your code can be rewrite as:
ndf['Entry'] = ndf['Revals'].where(signals_diff != 0).ffill()
Output:
Signals Revals Entry
0 1 101 101.0
1 1 102 101.0
2 1 105 101.0
3 1 104 101.0
4 1 105 101.0
5 0 106 106.0
6 0 107 106.0
7 0 108 106.0
8 0 109 106.0
9 0 109 106.0
Let us keep using the index position slice with get_indexer
for i,element in enumerate(signals_diff):
if (i==0):
ndf.iloc[i,ndf.columns.get_indexer(['Entry'])] = ndf.iloc[i,ndf.columns.get_indexer(['Revals'])]
elif (element == 0):
ndf.iloc[i,ndf.columns.get_indexer(['Entry'])] = ndf.iloc[i - 1,ndf.columns.get_indexer(['Entry'])]
else:
ndf.iloc[i,ndf.columns.get_indexer(['Entry'])] = ndf.iloc[i,ndf.columns.get_indexer(['Revals'])]

Displaying element in my text file when it is lesser than the range?

I am trying to create a Top 5 leaderboard for my game in Python 3.
Here's what I have
Top_Score = open("highscore.txt", "r+")
score_list = []
print(" Top 5")
print("==========")
for line in Top_Score.readlines(): # Read lines
score_list.append(line)
score_list.sort()
for i in range(5):
print("Pos", str(i + 1), ":", score_list[i])
print("==========")
Top_Score.close()
highscore.txt
50
18
20
40
50
60
70
Output
Top 5
==========
Pos 1 : 18
Pos 2 : 20
Pos 3 : 40
Pos 4 : 50
Pos 5 : 50
==========
But how can I display element in my text file if it is lesser than the range(5) without any errors? Any help would be appreciated
Example highscore.txt
50
18
20
Example Output
Top 5
==========
Pos 1 : 18
Pos 2 : 20
Pos 3 : 50
==========
In the print loop, you need to check if the size of the list is smaller than 5. If so, only loop until the size.
So, something like this:
loop_range = 5
if len(score_list) < loop_range:
loop_range = len(score_list)
for i in range(loop_range):
print("Pos", str(i + 1), ":", score_list[i])
This can be rewritten using the min function to select the smaller of the two numbers, 5 or the size:
loop_range = min(5, len(score_list))
for i in range(loop_range):
print("Pos", str(i + 1), ":", score_list[i])

How can I find the alphabetic position of the letters in a word and then add the numbers up?

I wrote some of the code which gives me the total of the word with the position of the English alphabet but I am looking for something that prints the line like this:
book: 2 + 15 + 15 + 11 = 43
def convert(string):
sum = 0
for c in string:
code_point = ord(c)
location = code_point - 65 if code_point >= 65 and code_point <= 90 else code_point - 97
sum += location + 1
return sum
print(convert('book'))
def convert(string):
parts = []
sum = 0
for c in string:
code_point = ord(c)
location = code_point - 65 if code_point >= 65 and code_point <= 90 else code_point - 97
sum += location + 1
parts.append(str(location + 1))
return "{0}: {1} = {2}".format(string, " + ".join(parts), sum)
print(convert('book'))
Heres the output:
book: 2 + 15 + 15 + 11 = 43
More info on string.format and string.join.

Python3 Extracting lines between first instance of two markers

I imported a text file from URL and want to process it. The file looks as below. There are two instances of " innings " and "Extras ". I want to extract lines between the FIRST instance of " innings " and FIRST instance of "Extras ". The code that I wrote extracts ALL instances. How do I resolve this?
Toss: Sri Lanka Umpires: M Erasmus (South Africa) and NJ Llong
(England) TV umpire: S Ravi (India) Match referee: DC Boon
(Australia) Reserve umpire: SD Fry (Australia) Player of the
match: CJ Anderson New Zealand innings (50 overs maximum)
R M B 4 6 MJ Guptill c Sangakkara b Lakmal
49 94 62 5 0 CJ Anderson c Lakmal b
Kulasekara 75 77 46 8 2
+L Ronchi not out 29 29 19 4 0
Extras (lb 2, w 8, nb 3) 13 Total (6 wickets, 50 overs, 226 mins) 331
Sri Lanka innings (target: 332 runs from 50 overs) R
M B 4 6 HDRL Thirimanne b Boult
65 90 60 8 0 RAS Lakmal not out
7 21 17 0 0
Extras (w 10, nb 1) 11 Total (all out, 46.1 overs, 210 mins) 233
Here is my code:
flag = 1
for line in data:
if " innings " in line:
flag = 0
print('')
if line.startswith("Extras "):
flag = 1
print('')
if not flag and not " innings " in line:
print(line)
Your program must stop on the first occurrence of Extras:
active = False # A variable `flag` is not very precisely named,
# better call it `active`, make it boolean
# and flip the values
for line in data:
if " innings " in line:
active = True # now we want to do things
print('')
continue # but not in this loop
if line.startswith("Extras "):
print('')
break # now we're done!
# alternative Method:
# active = False
if active:
print(line)
If you want to store all occurrences:
active = False
stored = []
for line in data:
if " innings " in line:
tmp = []
active = True # now we want to do things
continue # but not in this loop
if line.startswith("Extras "):
stored.append(tmp)
active = False
continue
if active:
tmp.append(line)
You'll end up with a list of lists of lines for further processing.

love2d - update the text of printed text

I have printed text that says "Yes". and I have to buttons in the shape of arrow. I am trying to get it so that if I click the left arrow it say "No" and if I click the right arrow is says "Yes".
fsdefault = "Yes"
fs = love.graphics.print(fsdefault, 440, 160)
love.graphics.draw(larrow, 425, 163)
love.graphics.draw(rarrow, 470, 163)
function love.update(dt)
function love.mousepressed( x, y)
if x > 424 and x < 435 and y > 161 and y < 172 then
fsdefault = "No"
end
if x > 275 and x < 320 and y > 305 and y < 325 then
fsdefault = "Yes"
end
end
end
How about something like:
local fsdefault = ""
function love.mousepressed( x, y)
if x > 424 and x < 435 and y > 161 and y < 172 then
fsdefault = "No"
end
if x > 275 and x < 320 and y > 305 and y < 325 then
fsdefault = "Yes"
end
end
function love.draw()
love.graphics.print(fsdefault, 440, 160)
love.graphics.draw(larrow, 425, 163)
love.graphics.draw(rarrow, 470, 163)
end
Note that for clarity, you should only perform screen drawing operations inside love.draw.
Also, try to avoid declaring functions inside love.update. That code snippet will make love redefine love.mousepressed every single frame of your game!

Resources