Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Yes or No Questions

Author
Message
BMX1997
14
Years of Service
User Offline
Joined: 12th Jan 2010
Location:
Posted: 9th Jun 2010 19:47
Hey, could someone explain to me how to make it so if say i wrote

Input answer$

And if the answer was "Yes" it'd do whatever, but if it was "No" it'd do something else. I know I could just make it so if the "Y" key was pressed it would so the same thing, but I can't do that for everything. So might as well learn to do it now. =D
tomaszavenger
15
Years of Service
User Offline
Joined: 24th Jun 2009
Location:
Posted: 10th Jun 2010 01:06
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 10th Jun 2010 20:39
Put lower$(answer$)="yes" to avoid upper and lower case problems.

http://s6.bitefight.org/c.php?uid=103081
tomaszavenger
15
Years of Service
User Offline
Joined: 24th Jun 2009
Location:
Posted: 10th Jun 2010 22:41
Good idea DVader, I forgot all about that!
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 11th Jun 2010 09:04
Quote: "I know I could just make it so if the "Y" key was pressed it would so the same thing, but I can't do that for everything. So might as well learn to do it now. =D "


Shortcuts and algorithms for multiple solutions are two big keys to programming. If not for the lower$() command (and if you didn't think to write one of your own), you would have to check for every combination, like "yeS" "YeS" "NO" "nO" etc. If you ever find yourself doing repetitive slightly modified code like that, re-examine what you're doing, and make sure it's absolutely necessary.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 11th Jun 2010 16:06
This code will handle an answer that is neither 'yes' or 'no'.



As an alternative, you could check the keystate of the y and n keys (21 and 49 respectively) to allow the user to make a choice.

LB
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 11th Jun 2010 16:58
Actually, using DBP's virtual keyboard might be a more reliable method of detecting whether "y" or "n" is pressed - scancodes might change from keyboard to keyboard. Try something like:


(help files example - should show 1 if you press F1 on your keyboard)

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."
BMX1997
14
Years of Service
User Offline
Joined: 12th Jan 2010
Location:
Posted: 11th Jun 2010 22:16
Hey LFBN. Thanks for the code you posted. Helped out a lot. I'm not exactly sure how to use those Case,Endcase,Select,etc. Are here any tutorials'guides that discuss that topid that you may know of?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Jun 2010 17:22
You're welcome.

Quote: "I'm not exactly sure how to use those Case,Endcase,Select,etc. Are here any tutorials'guides that discuss that topid that you may know of?"


Normally, I would point you to TDK's tutorials (look in the tutorial thread that is a sticky on this board), but I couldn't find any that addressed SELECT / ENDSELECT, so I will do my best to help you with it.

SELECT / ENDSELECT is normally used when you have a lot of comparisons to make and it makes your code a lot more readable and easier to understand. Within the SELECT / ENDSELECT code, you use the CASE / ENDCASE commands for the actual value comparisons. It is always wise to place a DEFAULT CASE in your code, because users don't always enter what they are supposed to (or values are different than you figured them to be) and it serves kindof like an error trap.

For example, if you want to code a program that asks a user to input a particular month, you would have to look at up to 13 different things; (the 12 months, plus the default case). This would be tedious with IF / THEN statements.

You can also use more than one value in the case statement. Like this:



In the months' example, you can use multiple case variables like this:



You can use Case / ENDCASE to branch to different parts of your program, like this:



Change the value of 'level' to see what happens.

I hope this is helpful. If you have any more questions, please let me know and I will try to help.

Good luck,

LB
BMX1997
14
Years of Service
User Offline
Joined: 12th Jan 2010
Location:
Posted: 12th Jun 2010 22:46 Edited at: 13th Jun 2010 01:28
Alright I made something like that last code you posted . BUT it has four levels instead of three and the Until 'day$ = "exit"' does not work. Not sure why. If i get rid of it afer the 1st level is over and you press a button it exits. And also for the 1st Case Default I put..

Case Default
If Level <> LevelOne then : Gosub LevelOne
If Level <> LevelTwo then : Gosub LevelTwo
If Level <> LevelThree then : Gosub LevelThree
If Level <> LevelFour then : Gosub LevelFour
Endcase
I don't know it thats right or not. Also whenever I start it it starts with level four. Not level one like it should. D= Heres the code..(Not sure how do use the Code snippets D=..

Level = 4
Center Text Screen Width() / 2,Screen Height() - 40,"Enter 'exit' to quit."
CLS
Select Level
Case 1
Gosub LevelOne
Endcase
Case 2
Gosub LevelTwo
Endcase
Case 3
Gosub LevelThree
Endcase
Case 4
Gosub LevelFour
Endcase
Case Default
If Level <> LevelOne then : Gosub LevelOne
If Level <> LevelTwo then : Gosub LevelTwo
If Level <> LevelThree then : Gosub LevelThree
If Level <> LevelFour then : Gosub LevelFour
Endcase
Endselect
Wait Key
End

LevelOne:
Input "Pick a day that begins with the letter 'T': ";day$
Day$ = Lower$(Day$)
Select Day$
Case "tuesday","thursday"
Print "You picked the right day."
Endcase
Case "monday","wednsday","friday","saturday","sunday"
Print "That does not start with 'T'."
Print "Try again."
Endcase
Case Default
Print "Please check your spelling and try again."
Endcase
Endselect
Return

LevelTwo:
Input "Pick a day that begins with the letter 'S': ";day$
Day$ = Lower$(day$)
Select Day$
Case "saturday","sunday"
Print "You picked the right day."
Endcase
Case "monday","tuesday","wednesday","thurseday","friday"
Print "That does not start with an 'S'."
Print "Try again."
Endcase
Case Default
Print "Please check your spelling and try again."
Endcase
Endselect
Return

LevelThree:
Input "Please pick a day that starts with the letter 'W': ";day$
Day$ = Lower$(day$)
Select Day$
Case "wednesday"
Print "You picked the right day."
Endcase
Case "sunday","monday","tuesday","thursday","friday","saturday"
Print "That does not start with a 'W'."
Print "Try again."
Endcase
Case Default
Print "Please check your spelling and try again."
Endcase
Endselect
Return

LevelFour:
Input "Please Select a day that starts with the letter 'F': ";day$
Day$ = Lower$(day$)
Select Day$
Case "friday"
Print "You picked the right day."
Endcase
Case "sunday","monday","tuesday","wednesday","thursday","saturday"
Print "That does not start with a 'F'."
Print "Try again."
Endcase
Case Default
Print "Please check your spelling and try again."
Endcase
Endselect
Return
If day$ <> "exit" then Wait Key
Until day$ = "exit
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Jun 2010 16:29


I am pressed for time at the moment. First off, when posting code, click the CODE button, paste your code and then press the CODE button again.

The word "exit" was used in the example code so that you could see how CASE / ENDCASE works, but normally you would not use it in a game. Also, please indent your code; it makes it much more readable and can help you trap errors in syntax. I have indented it for LevelFour as an example to you. Lastly, in your CASE - DEFAULT, you basically are going to have the code going to all four subroutines. If it gets there, it means that the values that CASE compared to do not match any of the conditions. I would suggest just using 1 as a default.

Hope this is helpful. I should be back on later today when I have more time and will check back with you.
BMX1997
14
Years of Service
User Offline
Joined: 12th Jan 2010
Location:
Posted: 13th Jun 2010 17:12 Edited at: 13th Jun 2010 17:59
Thank you. I'm not sure how do make it go through all four levels though. And your code works for level four that's it. And when I replace the errors in my code with yours mine doesn't work.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Jun 2010 21:13
Quote: "Thank you."

You're welcome.

Quote: "I'm not sure how do make it go through all four levels though."


Here is an example that shows it going through all four levels. It also will contain values 0 and 5, which will result in it going to the case default. I put this in so you could see how it works.



Quote: "And your code works for level four that's it."


That is because level is initially set to equal 4. Since it is the value of level that we are checking, whatever it is will determine which case / endcase instructions will be executed. So, in the other program if you changed the value of level to 1, it would always branch to level 1.

Run the code and see if it makes sense to you. I added some colors to the text just to break up the monotony a little.
BMX1997
14
Years of Service
User Offline
Joined: 12th Jan 2010
Location:
Posted: 13th Jun 2010 21:49 Edited at: 13th Jun 2010 21:52
Hey. Thankyou very much. I used my regular code and just fixed it up. Works perfect thank you very much. I understand everything but the 'okay' parts and the 'ink yellow,0 : print "At default (level 1) -- cycle = " + str$(cycle)'. I tried taking stuff out of it to see if it still worked and how it worked but I don't understand it. D=
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Jun 2010 22:22
The variable, okay, simply is being used as flag to tell whether or not the code should move forward. If the value held in okay is 0, the program continues to loop, but if it equals (whatever I set it to: 1, 12 or 7) then the loop is closed and it continues processing. I used different values on purpose to show that you can create the conditional check that you want.

The variable doesn't have to be 'okay', you could use whatever you want. For example, you can use the word 'finished' in place of okay. Bear in mind, there are other ways to accomplish this same task. This works for me, and is fairly easy to understand, so that is why I use it. Plus, I don't have to check for multiple days at the end of the repeat-until loop, I only have to look at the value of okay.

Quote: "ink yellow,0 : print "At default (level 1) -- cycle = " + str$(cycle)"


'ink yellow,0' sets the text color to yellow, which I defined at the start of the program. I assigned the RGB value of yellow to a variable of the same name. The 0 after it denotes the background drawing color; 0 being black (i.e. transparent).

By printing out that the program is at the default case, it lets you know where it is at in the program. Cycle is the variable I used to cycle through the values 0 to 5. I set level to equal whatever the value of cycle is. By using 'str$(cycle)' I am telling the computer to print the value of cycle on the screen. This is how you will know if it is 0 or 5; both will send it to your default case. By printing the value, you see what caused it to go there specifically.
BMX1997
14
Years of Service
User Offline
Joined: 12th Jan 2010
Location:
Posted: 14th Jun 2010 01:46
Hey thanks again. I understand the Okay part, but not the "ink yellow,0 : print "At default (level 1) -- cycle = " + str$(cycle)". Is it all necessary? Like the Print and stuff. Could you show me what is and what is not?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 14th Jun 2010 05:06
Quote: "Is it all necessary? Like the Print and stuff. Could you show me what is and what is not?"


No, it's not necessary. It merely was put in to show you that the program was executing the code within the DEFAULT CASE code. I am trying to help you understand how it works. When the value within the variable that is selected (in this case, the value of level is selected) does not match any of the other CASE - ENDCASE criteria, it goes through the DEFAULT CASE code. You can put anything in there that you want; I only put this in as an example.

I had the code to print the cycle value, as level is made equal to cycle at the start of every loop. The value of level is changed in the DEFAULT-CASE, so I used another variable (cycle) to keep track of it. When cycle = 0 or cycle = 5, it will execute the DEFAULT-CASE code, as those two values will not match any of the other case statements.

I made it yellow just to make it a little more colorful.

I hope this helps.

Login to post a reply

Server time is: 2024-11-24 16:09:33
Your offset time is: 2024-11-24 16:09:33