I have a TI-84 Plus CE, and I'm trying to create a random integer dice rolling program. For my code I have:
ClrHome
Menu("HOW MANY SIDES?","4",A,"6",B,"8",C,"10",D,"12",E,"20",F)
Lbl A
randInt(1,4,1)➔G
Disp G
Stop
Lbl B
randInt(1,6,1)➔H
Disp H
Stop
Lbl C
randInt(1,8,1)➔I
Disp I
Stop
Lbl D
randInt(1,10,1)➔J
Disp J
Stop
Lbl E
randInt(1,12,1)➔K
Disp K
Stop
Lbl F
randInt(1,20,1)➔L
Disp L
Stop
I'm expecting:
HOW MANY SIDES?
1:4
2:6
3:8
4:10
5:12
6:20
And then a response like {8}. I assume the issue is with the randInt portions of the program, but I don't know how to fix it. I'd appreciate if anyone can help out with this. Thanks!
can lists be diplayed with the Disp command ? Instead of a variable, you have lists of length one. Have you tried randInt(1,6)->H instead of randInt(1,6,1)->H ?
I tried running the code as it is. It works fine, so I guess I'd check for typos in your program. Missing or flipping values in the long list of menu arguments would produce an ARGUMENT error before the menu opens (is that when the error happens? vs. after selecting a menu option).
I recommend using 4, 6, 8... instead of A, B, C... for the labels to both reduce the risk of typos and reduce the necessary mental work.
Related
The 1..T function is not working for me, and I don't know why. My code looks like this:
set TIME;
data;
set TIME = 1..8760 by 3;
display TIME;
Here I want it to display 1, 4, 7, 10, and so on, but it just goes 1..8760 by 3. How can I fix this so that I don't have to write 8,760 different numbers?
Thankful for answers!
I don't find any error in your code. I tried in my AMPL IDE:
AMPL IDE
I suppose that when you write "data" is that your code is in the .dat file and when you write "display" is your .run file
What are you using to write your model?? NEOS SERVER or the IDE??
Regards!
Try deleting the first two lines of your code.
It looks as if the format you're using to specify TIME only works in "model" mode; in "data" mode, the same text is interpreted as declaring a set of literals "3", "by", and "1..8760". (Quick test: if you type display card(TIME); you get a value of 3, telling you that there are exactly 3 members in this set.)
Section 5.2 of the AMPL Book recommends using a "x..y by z" type declaration in the model (with x, y, z as declared params) and then specifying values for x, y, z in the data.
Note that starting at 1 and increasing in steps of 3 won't actually get to 8760 exactly, so you might want to change your start to 0 or 3, or your end to 8761, if you want equal gaps between the numbers.
When I run this in Revolution R Enterprise, it totally crashes Rstudio on the last line:
require(RevoScaleR)
set.seed(1)
a = sample(c("happy", "sad", "other", NA), 100, replace = TRUE)
y = data.frame(a)
y$a = as.character(y$a)
rxCrossTabs(1 ~ a, data = y)
This seems buggy to me. Shouldn't it at least throw a warning and return me the command prompt?
The quick fix above is to simply exclude the line y$a = as.character(y$a), but I'd rather really understand what is going on. I'm having the same sort of crash with rxCrossTabs when I try to run it on a .xdf file, and I wonder if it is a related issue. I.e., perhaps somehow R is reading in a column of the file as character only instead of as factor, but I'm not sure how to investigate that directly.
This is not a Revolution R problem; it's an Rstudio problem. If I run your code in R in a terminal session, it does not crash, but it does throw an error message that Rstudio should have relayed to you:
Evidently your challenge now is to figure out how to put the right variable type on each column of interest in your .xdf file. Specifically, you need to have a factor variable to use the rxCrossTabs function.
Hi i have a weird problem with Erlang on Windows i am running 16B and WinXP.
I have the following code
-module(test).
-export([cost/1,total/1]).
cost(orange) ->
5;
cost(apple) ->
6.
total(L) ->
[cost(I) * Q || {I,Q} <- L].
I run it with
test:total([{orange,2}]).
and it gives me "\f"
changing cost(I) * Q to use -,+ or divide gives me a number.
I have no idea why multiply dosen't work in list comprehension. Running
[test:cost(I) * Q || {I,Q} <- [{orange,2}]]
in an erlang console and emacs mode also dosen't work but
test:cost(orange) * 2
does give me a number.
Any ideas why?
Note your cost/1 function returns a number. But total/1 returns a list (of numbers).
The results on that list are ok, this is just how erlang happens to display lists of small integers. See http://www.erlang.org/faq/problems.html 9.3
to see what I mean, try with larger numbers
test:total([{orange,2000}]).
Again, this is just a display issue, the value in the lists are what you expect. Try it:
[Value] = test:total([{orange,2}]).
Value.
A string is a list of integers. The value you're returning is a list of integers.
Erlang uses a simple heuristic for when to show something as a string, or as a list of integers: is it a flat list containing only numbers in the range {55,250}. (I made those numbers up, but it's something like that. If there are control characters or low characters, it bails.)
Since Erlang doesn't do this to tuples, tuples make it easy to see.
1> {72,101,108,108,111,44,32,83,116,101,112,104,101,110,46}.
{72,101,108,108,111,44,32,83,116,101,112,104,101,110,46}
2> [72,101,108,108,111,44,32,83,116,101,112,104,101,110,46].
"Hello, Stephen."
Erlang is just guessing wrongly what's inside the list.
HTH.
I'm a beginner to Python and I'm having some trouble with this. I have to make a for loop out of this problem. Can anyone explain how I would go about this?
nextNValues (startValue, increment, numberOfValues)
This function creates a string of numberOfValues values, starting with startValue and
counting by increment. For example, nextNValues (5, 4, 3) would generate a string of
(not including the comments):
5 - the start value
9 - counting by 4, the increment
13 - stopping after 3 lines of output, the numberOfValues
You could use range(startValue,startValue+(increment*numberofValues),increment).
for i in range(numberOfValues):
print startValue + i * increment
I am not sure if that is exactly what you are looking for... but it is my suggestion based on the information you have posted.
It would probably be easiest to write a for loop with an index like i and use that to add i*increment ti start value and save the resulting value to a list. Have the loop run numberOfValues times. If this is homework it would be better for you to write out the actual code for yourself
I'm extremely new to Visual Studio 2010.
For my class, I need to make a calculator. The requirement for this problem is that if A or B is zero, then for the program to not actually calculate A or B; instead, the program assigns the value of the variable that is not zero to eh result, and if they're both zero, then just make the answer zero.
Here is my code:
Public Class Form1
Private Sub AddBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
Dim A As Decimal
Dim B As Decimal
Dim Result As Decimal
A = ABox.Text
B = BBox.Text
A = Decimal.Parse(ABox.Text)
B = Decimal.Parse(BBox.Text)
If (ABox.Text = 0) Then
ResultLabel.Text = BBox.Text
End If
If (BBox.Text = 0) Then
ResultLabel.Text = ABox.Text
End If
If (BBox.Text = 0 And ABox.Text = 0) Then
ResultLabel.Text = 0
End If
Result = A + B
ResultLabel.Text = Result.ToString("N2")
End Sub
My questions are as follows:
Are if statements good for this, or would Try/Catch be better?
Since the answer will automatically be right, even if the code is incorrect (e.g. 9+0 will be nine regardless, whether or not the If or Try/Catch actually works), they key is proper step by step debugging. What's the optimal way to do this? I had the one menu displaying everything step by step with how the program functioned before, but for some reason, I can't seem to find the window after I closed it. I want to see it step by step as I debug with break points.
Any other tips on good syntax for these type of conditional operators?
Sorry for sounding stupid; I couldn't find the topics on here that would cover this type of problem.
OK...first off, you don't really need the Ifs at all. (Identity Property of Addition: For any x, x + 0 = x.) Any error that's going to happen is going to happen before you even get to them, so the only thing you've done is say that if ABox parses to zero and BBox's value is something like "5.000000", all those zeros will get copied into the result. Likewise if the boxes are reversed.
Also, if you use Decimal.TryParse instead of Decimal.Parse, un-number-like stuff in the input boxes won't kill your program. They'll just get turned into 0.
You'd get similar results (minus the extra zeros) with some code like
Private Sub AddBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
Dim A as Decimal, B as Decimal
Decimal.TryParse(ABox.Text, A)
Decimal.TryParse(BBox.Text, B)
ResultBox.Text = (A + B).ToString("N2")
End Sub
If for some stupid reason you really, really, really need all that If crap, you will want to use ElseIf to ensure that the default case doesn't run.
Private Sub AddBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
Dim A as Decimal
Dim B as Decimal
Decimal.TryParse(ABox.Text, A)
Decimal.TryParse(BBox.Text, B)
If A = 0 then
ResultBox.Text = BBox.Text
ElseIf B = 0 then
ResultBox.Text = ABox.Text
Else
Dim result as Decimal = A + B
ResultBox.Text = result.ToString("N2")
End If
End Sub
But it's a waste -- and what's worse, if the point is to add two numbers, it's incorrect. Someone could put "I Like Cheese" in the first box and "YAAAAAAAAY!!!" in the other, and the result would be "YAAAAAAAAY!!!". Clearly not what should happen.
Private Sub AddBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles AddButton.Click
Dim A as Decimal
Dim B as Decimal
Try
A = Decimal.Parse(ABox.Text)
B = Decimal.Parse(BBox.Text)
ResultBox.Text = (A + B).ToString("N2")
Catch ex as FormatException
MessageBox.Show("Exactly two numbers (one per box), please!")
End Try
End Sub
But i detest doing that for user input. You should expect users to be malicious and/or idiotic, and count on them doing everything they can to mess up your app. It'll save you lots of debugging later on. But if we assume the worst, is malformed input really an exceptional condition? I say no. (Others may disagree. But they're wrong. :) )
If you want to notify the user of an input error, Decimal.TryParse also returns a boolean (true/false) saying whether it succeeded. If it failed, you can show a message telling the user to quit being an idiot. (Slightly more diplomatically, if you want.)
Secondly...turn Option Strict on. Please. All this implicit casting gives me the willies. Note that that will probably add like a dozen errors to your list...but that's a good thing. One should be aware when they're trying to put a number in a string variable and so on, cause lots of magic happens there that (in my opinion) people need to be more aware of.
K, now, as for debugging...if you've followed my advice up to this point, you shouldn't really need to debug. The exceptions are gone, and the code's going to be more correct before you can even compile it. But if you still have to, one way to go step by step through the program is to set a breakpoint on either the Function line itself, or the first line that isn't a declaration. (Click the left margin next to the line. A red ball should appear in the margin about where you clicked. That lets you know there's a breakpoint there.) You can probably try to set a breakpoint on the declarations too, but i seem to remember that causing the breakpoint to be either on the next line that actually does something, or on the beginning of the function. I forget which, and don't have VS on this machine to check.)
When you run the program from VS, the program will start, and when it hits the breakpoint, VS will pop back into the foreground with a yellow arrow where the red ball is. That yellow arrow points at the next line to be executed. From there, you can use the debug toolbar (look in your toolbars; you should have extra buttons, blue i think, that look like play/stop/etc buttons) to step through the code. You should also see windows labeled 'Locals', 'Watches', and other windows you'd expect to see in a worthwhile debugger. :)
Just be aware that while the program's stopped at a breakpoint, or stepping through the code, the app will appear frozen. UI updates probably will not appear right away, and consequently, you won't be able to type anything in the input boxes or hit the button til you resume (hit the "play' button).