Stupid BASIC Noob Question Time

Basic and Machine Language

Moderator: Moderators

User avatar
Pedro Lambrini
Vic 20 Scientist
Posts: 1132
Joined: Mon Dec 01, 2008 11:36 am

Stupid BASIC Noob Question Time

Post by Pedro Lambrini »

Okay, I haven't written any code in about 20 years so bear with me! I want to write something where the user can only choose one of two options. I have typed in this:

Code: Select all

10 input"1 or 2";a
20 if a=1 then gosub 200
30 if a=2 then gosub 210
40 print"well done!"
50 goto 10
200 print"you chose left"
205 return
210 print"you chose right"
215 return
It seems to work but if I type anything other than "1" or "2" nothing really significant happens and the code carries on running. How would I write the code so that only one of the two options are acceptable? I know that with numbers I could use the < > signs to stop numerical values but what about letters or PETSCII??

I hope what I'm asking is clear!! Thanks in advance. :)
"...That of the Eastern tribe being like a multitude of colours as if a rainbow had settled upon its brow..." Daniels 1:3
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

You can separate the whole input routine into an own sub-routine. And, in that case, use GET instead of INPUT. Then the code could look like this:

Code: Select all

10 GOSUB 100
20 IFA$="1"THENPRINT"YOU CHOSE LEFT"
30 IFA$="2"THENPRINT"YOU CHOSE RIGHT"
40 PRINT"WELL DONE!"
50 GOTO 10
100 PRINT "1 OR 2?"
110 GETA$:IFA$="1"ORA$="2"THENRETURN
120 GOTO 110
Effectively, the GOSUB 100 only returns, when an acceptable key has been pressed. Otherwise, the sub-routine will remain in its loop.

You can change the acceptable keys within the IF statement in line 110, and alter the PRINT statement the reflect the change.

The PRINT statements in the lines 20, and 30 of course only serve illustrational value, you must decide what actions should be taken, and these, again could be delegated to sub-routines again.

Greetings,

Michael
User avatar
Pedro Lambrini
Vic 20 Scientist
Posts: 1132
Joined: Mon Dec 01, 2008 11:36 am

Post by Pedro Lambrini »

Thanks very much. I'll play around with this for a wee while. :)
"...That of the Eastern tribe being like a multitude of colours as if a rainbow had settled upon its brow..." Daniels 1:3
Leeeeee
soldering master
Posts: 396
Joined: Fri Apr 23, 2004 8:14 am

Post by Leeeeee »

Or, just to confuse you further..

Code: Select all

10 INPUT "1 OR 2"; A
20 IF A=1 THEN GOTO 200
30 IF A=2 THEN GOTO 210
40 PRINT "YOU CHOSE NEITHER LEFT NOR RIGHT!"
50 GOTO 10
60 PRINT "WELL DONE!"
70 GOTO 10
200 PRINT "YOU CHOSE LEFT"
205 GOTO 60 
210 PRINT "YOU CHOSE RIGHT"
215 GOTO 60
.. and ..

Code: Select all

10 INPUT "1 OR 2"; A
20 ON A GOTO 200,210
30 PRINT "YOU CHOSE NEITHER LEFT NOR RIGHT!"
40 GOTO 10
60 PRINT "WELL DONE!"
70 GOTO 10
200 PRINT "YOU CHOSE LEFT"
205 GOTO 60 
210 PRINT "YOU CHOSE RIGHT"
215 GOTO 60
Lee.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Personally I prefer INPUT with strings A$ and then apply some VAL() or possibly ASC() function to get a numerical value from it. In that way you won't get ?REDO FROM START if the users inputs a non-numerical value.

Code: Select all

10 input "1 or 2";a$
15 a=val(a$):f=1
20 on a gosub 200,210
25 on f gosub 100,110
30 goto 10
100 print"you chose neither left nor right!":return
110 print"well done!":return
200 print"you chose left":f=f+1:return
210 print"you chose right":f=f+1:return
In this example, variable F acts like a fail flag. Depending on how your actual program turns out, the amount of error handling may differ. Of course if you're making any program with a bit of action, the GET syntax Mike mentions is preferrable over INPUT.
Anders Carlsson

Image Image Image Image Image
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

Just curious. Why did you F=F+1 instead of just F=1 for on or off?

Overall, I think I would have done the whole task differently, but I don't want to further complicate things. The above examples answer the question.
High Scores, Links, and Jeff's Basic Games page.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Good question. The ON X GOTO/GOSUB will only branch on X>0, so F=0 would bypass both the error message and the greeting. Thus F=1 from the beginning would assume the user made a mistake, and setting it to 2 (in this case F=F+1) simply upgrades the branch from error to greeting.
Anders Carlsson

Image Image Image Image Image
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

But if F=1 from the beginning as a fail flag, you could just say F=0 to change the fail flag and it would reset to one in the next prompt. That would 8 bytes. 9 if you lose the semicolon in line 10. :-P

Code: Select all

10 input "1 or 2"a$
15 a=val(a$):f=1
20 on a gosub 200,210
25 if f gosub 100
30 print"well done!":return
40 goto 10
100 print"you chose neither left nor right!":return
200 print"you chose left":f=0:return
210 print"you chose right":f=0:return
High Scores, Links, and Jeff's Basic Games page.
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Jeff-20 wrote:[...] 9 if you lose the semicolon in line 10. :P

Code: Select all

10 input "1 or 2"a$
... which gives you a '?SYNTAX ERROR'. :P

Anyway, this supposedly simple user request dialogue can obviously be programmed in thousandth of ways. Pedro just didn't get it right at the beginning, how to react in case the user input isn't valid.

Then the program can be designed in two ways: Either ignore invalid input, or report to the user, that the input is invalid. If the possible choices are presented on screen, IMO it isn't necessary to notify the user in case not one of the allowed inputs was done - i.e., ignore.

And of course there are better, and nicer ways to present the user with two or more alternatives to choose from. Like highlighting a menu entry by printing it in inverse script, much like it is done in VICtoria Gold. f1/f7 select an entry, pressing RETURN confirms.
User avatar
Pedro Lambrini
Vic 20 Scientist
Posts: 1132
Joined: Mon Dec 01, 2008 11:36 am

Post by Pedro Lambrini »

Jeff-20 wrote:But if F=1 from the beginning as a fail flag, you could just say F=0 to change the fail flag and it would reset to one in the next prompt. That would 8 bytes. 9 if you lose the semicolon in line 10. :-P

Code: Select all

10 input "1 or 2"a$
15 a=val(a$):f=1
20 on a gosub 200,210
25 if f gosub 100
30 print"well done!":return
40 goto 10
100 print"you chose neither left nor right!":return
200 print"you chose left":f=0:return
210 print"you chose right":f=0:return
Right, okay, I think I follow this but I'm kinda with Jeff. I don't understand why F starts as 1 rather than the 0 which would be my assumption.

PS: Thanks for all these exapmles, they're all aiding in my learning. :)
"...That of the Eastern tribe being like a multitude of colours as if a rainbow had settled upon its brow..." Daniels 1:3
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

Mike wrote:
Jeff-20 wrote:[...] 9 if you lose the semicolon in line 10. :P

Code: Select all

10 input "1 or 2"a$
... which gives you a '?SYNTAX ERROR'. :P
Oh, right. Stupid me. I haven't used INPUT in a long time. I thought it could be handled like PRINT.

Incidentally, I had my first ever

?ILLEGAL DIRECT
ERROR


I had never even heard of that one before! Over two decades and I'm still discovering things about this machine!
High Scores, Links, and Jeff's Basic Games page.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Well, my case of thinking was that more advanced subroutines would be called upon successful or incorrect input. Perhaps whole moves will be performed, routines that may even get called from other parts of the main program. In that case, it would make more sense to break those routines into subs and make sure to ON .. GOSUB both in case of success or failure.

Code: Select all

5 tr=10:hu=10
10 print"you are followed by a troll. you have the following options:"
11 print"(1) fight the troll"
12 print"(2) flee into the forest"
13 h1=0:h2=0:input "your choice";a$ 
15 a=val(a$):f=1 
20 on a gosub 200,210 
25 on f gosub 100,110 
30 hu=hu-h2:if hu>0 and tr>0 then 10
35 if tr>0 then print"the troll still stands up"
37 if hu>0 then print"you defeated the troll"
40 if tr<=0 and hu<=0 then print"both of you died!"
50 end
100 print"the troll hits you!":h2=int(rnd(1)*5)+1:return
110 tr=tr-h1:if tr>0 and rnd(1)>0.7 then 100
115 return
200 print"you hit the troll in its face!":f=f+1:h1=int(rnd(1)*5)+1:return
210 print"you escape into the bushes!":f=f+1:h1=0:return
In this little .. uh, game .. you can fight the troll. If you do nothing, the troll is sure to hit you repeatedly. Even if you fight or flee, there is a 30% chance the troll will come after you and give you a smackdown.

On lines 200-210, action is taken depending on your move. The subroutines at lines 100 and 110 however deal with the effects after the action is taken. Surely line 100 could have been incorporated into the main program, but it would have been uglier to do the branch for troll hitting back or code would have needed to be repeated.
Last edited by carlsson on Fri Jan 22, 2010 1:40 am, edited 1 time in total.
Anders Carlsson

Image Image Image Image Image
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

I didn't make my question clear. If F is a flag, why did you F=F+1, instead of F=1? It seems like F would only be 0 or 1.
High Scores, Links, and Jeff's Basic Games page.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Ok, perhaps bad choice of word. Forget "flag", call it selector instead. Of course I might write ON F+1 GOSUB .. but it is almost even more cryptic.
Anders Carlsson

Image Image Image Image Image
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

Oh, I see what you're doing. I'm a little slow.
High Scores, Links, and Jeff's Basic Games page.
Post Reply