#21✎ 136TheV360Intermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthFirst DayJoined on the very first day of SmileBASIC SourceWebsiteNight PersonI like the quiet night and sleep late.Express YourselfSorry, I'm super picky about code styling.
Here's a slightly more efficient version of Shelly's code:
INPUT "Enter Password";PASSWORD$
VARIABLE=0
IF PASSWORD$=="STRING" THEN
IF VARIABLE THEN
GOTO @LOOPONE
ELSE
GOTO @LOOPTWO
ENDIF
ENDIFPro tip: in SmileBASIC, there's no such thing as a boolean variable type, so
TRUE = 1
FALSE = 0
and IF VARIABLE THEN is the same as IF VARIABLE>0 THEN
#22✎ 23CrazyblobBeginner ProgrammerI'm just starting out! I'm still trying to grasp the basics.Programming StrengthVideo GamesI like to play video games!HobbiesMinecraft Is Awesome!I love Minecraft!Express YourselfOhhhh, thank you!
Posted
#23✎ 80niconiiVideo GamesI like to play video games!HobbiesExpert ProgrammerProgramming no longer gives me any trouble. Come to me for help, if you like!Programming StrengthDrawingI like to draw!HobbiesActually, IF VARIABLE THEN is the same as IF VARIABLE!=0 THEN. Any number that isn't 0 counts as true, including negative numbers.
Posted
#24✎ 42FFVDGamesIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthSecond YearMy account is over 2 years oldWebsiteStar Wars Is Awesome!I love Star Wars!Express Yourself
Yup, thank you guys. I'm working on a text adventure type of game, so that's why I've been asking for this info.. and cuz I'm new to programming.
ayy I'm doing that too but on and off cuz BASIC bad and slowly becoming an adult plskillme
Posted
#25✎ 23CrazyblobBeginner ProgrammerI'm just starting out! I'm still trying to grasp the basics.Programming StrengthVideo GamesI like to play video games!HobbiesMinecraft Is Awesome!I love Minecraft!Express YourselfYeah basics pretty old.. I guess I better start learning python
Posted
#26✎ 152spaceturtlesVideo GamesI like to play video games!HobbiesAvatar BlockI didn't change my avatar for 30 days.WebsiteIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthBtw if you want you can use DEF'd statements instead of GOTO because DEF is less likely to mess up somewhere and can be neater.
Posted
#27✎ 23CrazyblobBeginner ProgrammerI'm just starting out! I'm still trying to grasp the basics.Programming StrengthVideo GamesI like to play video games!HobbiesMinecraft Is Awesome!I love Minecraft!Express YourselfHow's it less likely to mess up?
Posted
#28✎ 152spaceturtlesVideo GamesI like to play video games!HobbiesAvatar BlockI didn't change my avatar for 30 days.WebsiteIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthLumage showed something using an IF statement and a GOTO. Not really a big deal I think.
Posted
#29✎ 51seggiepantsThe GOTOs were bugging me too .... a lot.
VAR RETRIES = 0, LOCKED = FALSE, PW$
VAR VALID_PASSWORD$[0]
'READ THE LIST OF VALID PASSWORDS
READ PW$
WHILE PW$ != ""
PUSH VALID_PASSWORD$, PW$
READ PW$
WEND
PW$ = GET_PASSWORD$()
IF PW$ == "V360" THEN PRINT "MASTER, .... IT'S YOU!"
IF PW$ != "" THEN
LOG_IN
ELSE
PRINT "GAME OVER"
ENDIF
END
DEF GET_PASSWORD$()
VAR PW$, I, VALID
IF LOCKED THEN
PRINT "ACCESS DENIED"
ELSE
VALID = FALSE
REPEAT
INPUT "PASSWORD"; PW$
'IS THAT A VALID PASSWORD?
FOR I = 0 TO LEN(VALID_PASSWORD$) - 1
IF PW$ == VALID_PASSWORD$[I] THEN
VALID = TRUE
BREAK
ENDIF
NEXT I
IF VALID == FALSE THEN
RETRIES = RETRIES + 1
PRINT "INVALID PASSWORD."
IF RETRIES > 3 THEN
LOCKED = TRUE
PW$ = ""
PRINT "TOO MANY FAILED LOGINS. LOCKED!"
ENDIF
ENDIF
UNTIL VALID OR LOCKED
ENDIF
RETURN PW$
END
DEF LOG_IN
PRINT "C:>"
END
@PASSWORDS
DATA "PASSWORD", "V360", ""
Here is my overly long take with some tricky edge cases to puzzle over.
Note that the password list is data driven. You can add, edit, and delete passwords without altering the code outside of the data statement.
The pw$ variable in get_password and the one at global scope are different variables with the same name. Spooky.
If you call get password after your retries are up it will kick you out.
The while/wend, repeat/until and for loops save you from making a bunch of line labels as do the functions. You could move the code into new programs and reuse it now.
Get_password has () in the call because it returns a value. Log_in doesn't return a value so you don't use ().
Posted
#30✎ 816Lumageor you could just not complicate things for someone clearly trying to learn
Posted
#31✎ 51seggiepantsIt was actually designed to be useful to someone just starting out and trying to learn.
GOTO has been considered harmful to writing clean code since the 70s. If you are just starting out using GOTO/GOSUB will hurt you in the long run and naturally lead to unmaintainable spaghetti code. In the example I showed WHILE/WEND, FOR/NEXT, and a REPEAT/UNTIL loops. I even put in an decent example of BREAK to skip unneccesary processing. I showed the different DEF syntax for a function and a subroutine too. Learning those will help you write cleaner, more maintainable, more reusable, more readable code.
There is a lot to study packed into less than 60 lines, but I do think it is worth studying.
Posted
#32✎ 80niconiiVideo GamesI like to play video games!HobbiesExpert ProgrammerProgramming no longer gives me any trouble. Come to me for help, if you like!Programming StrengthDrawingI like to draw!HobbiesI agree with Lumage. Throwing a huge pile of concepts at someone all at once, while telling them the stuff they do understand is bad and wrong, is not a good way to teach someone.
Yeah, GOTOs can contribute to hard-to-read code over time compared to more advanced control flow, but this is not an immediate problem that has to be addressed when they're still learning basic concepts like comparison operators. Program structure and code maintenance can be worried about later.
It also strikes me as odd that this is all in the name of cleaner and more readable code, because the code you've given is fairly convoluted and difficult to follow. GET_PASSWORD$() could be rewritten as something like this:
DEF CONTAINS(ARRAY[], ITEM)
VAR I
FOR I=0 TO LEN(ARRAY)-1
IF ARRAY[I]==ITEM THEN RETURN TRUE
NEXT
RETURN FALSE
END
DEF GET_PASSWORD$()
VAR TRY,PW$
IF LOCKED THEN
PRINT "ACCESS DENIED"
RETURN ""
ENDIF
FOR TRY=1 TO 3
INPUT "PASSWORD";PW$
IF CONTAINS(VALID_PASSWORD$,PW$) THEN
RETURN PW$
ELSE
PRINT "INVALID PASSWORD."
ENDIF
NEXT
PRINT "TOO MANY FAILED LOGINS. LOCKED!"
LOCKED=TRUE
RETURN ""
END
Posted