i am new to python and learning it for my job functions. im following a very basic beginners tutorial and most of it looks very familiar and similar to other languages ive used. but... when i do the very simple
print('hello world')
i get the expected response of
hello world
but when i do another simple task:
x = 5
print('x is', x)
i get:
('x is', 5)
the print command is preserving the parentheses and single quotes and for the life of me i cant figure out why.
Since you're using Python 2.6, print is a statement and not a function. As a result, the arguments are not expected to be in parentheses (note that this has changed in version 3.0).
This code will do what you intended:
print 'x is', x
Your original code actually creates a tuple and prints it.
The use of print statement is echoing the type to string. ('x', 1) is a tuple.
Python evaluates ('x is', 5) and then turns the results (a tuple) into a string, which is ('x is', 5). Leave out the parentheses to get what you want:
print 'x is', 5
See the Python print documentation for more.
Related
when i write method missing in Object class i'm getting the output different in each interface.
the code is
class Object
def method_missing(hgh)
puts self
end
end
when i use REPL like irb, i get
when i use the command line, i get no error, any reasons would be helpful, thanks in advance
The tl;dr answer is that both are correct. Just more stuff happen in the REPL.
When you run the code from the command line like:
ruby script.rb
All that happens is that it's evaluated.
Whereas REPLs like IRB read your input, evaluate it and print it in a loop.
In this case evaluating your code literally broke the REPL and resulted in the subsequent print failing.
Now you may be a bit confused by this. "There is a print in both cases, I use puts!". The print I'm referring to here is the result that gets visualised after each evaluation. In this case the method definition result (=> :method_missing).
It might not only be the printing itself. It can be the ton of other under the hood code that the REPL has to execute to keep state like "what code was defined on which line" and so on.
Think of what you just did - you made it so that every object has every method possible to return nil. That is not just for the code you write in the REPL. It's for the code of the REPL itself as well.
I was doing a code golf (use the minimum number of characters) and I had the following working Python solution. I was trying to shorten my code by re-writing it to Ruby but my Ruby code would always print false.
The code had to read two strings, to ignore the case and to tell whether it was possible to obtain one string by rotating the other string. The output had to be either true or false. Do you have any idea what I did wrong in Ruby?
Python 3 (64 characters) - Works
a=input().lower()
b=input().lower()
print(str(a in 2*b).lower())
Ruby (47 characters) - Always prints "false"
a=gets.upcase
b=gets.upcase
p (b*2).include? a
With the examples I can think of, the Ruby code works correctly, but for some reason, it didn't work on the code golf site (codingame.com, the problem was proposed by user "10100111001").
In Ruby gets includes the \n at the end. You'd have to .chomp it away before doing anything.
a=gets.chomp.upcase
b=gets.chomp.upcase
p (b*2).include? a
By the way, this is not the right way to "tell whether it was possible to obtain one string by rotating the other string", it only partially solves the problem, hope you know that.
In most programming languages, you can mix and match strings with variables during output. However, I can't seem to find a good way to do so. Here is my code:
Prompt A,B
√(A^2+B^2)->C
If iPart(C)≠C
Then
Disp "C = √(",C
Else
Disp "C = ",C
End
Goto ED
Label ED
Unfortunately, with this code, it ends up printing like so:
A? 3
B? 5
C = √(
34
Done
This is not what I want. I would love to be able to have it print C = √(34), but I currently can't find any way to mix variables and strings. Any help would be appreciated.
I know this is a little late, but it might help others as well.
The Output(... command would be used in this case.
Prompt A,B
√(A^2+B^2)->C
If iPart(C)≠C
Then
Disp "C = √(",C
Output(3,7,C
Else
Disp "C = ",C
End
Just remember that the home display is 16x8 characters, which you may need as you plan out how and where to display your results.
Unfortunatelly the "string" command suggested by PGmath doesn't exist on the Ti-83/84/85/86. Actually there is no function for converting a number into a string.
But a possible solution is given here:
http://tibasicdev.wikidot.com/number-to-string2
In ti-basic for the ti-83 the plus (+) is used to concatenate strings. Like this:
Disp "foo"+" "+"bar"
Will output:
"foo bar"
You must remember to convert numbers to strings using string() though:
Disp "C=√("+string(c)+")"
Will output:
"C=√(34)"
Disp "C=√("+c+")" (no string()) will throw an error.
Since version 5.2.0 the ti-83 and 84 (possibly others as well) got the toString( command which can be used to turn a variable into a string. This piece of code will display the variable C with the correct text on the screen.
Disp "Variable C: "+toString(C
Make sure your calculator is using this version though, otherwise you're going to have a hard time finding this command.
I know this thread is very dead but for posterity:
If you have a TI-84+CE on version 5.2 or later, you can use the toString( function.
If you do not, if the output string will always be the same size, simply use Output(. If this does not generate the desired effect, you can use:
:{0,.5,1→L₁
:NL₁→L₂
:Med-Med Y₁
:Equ►String(Y₁,Str1
:sub(Str1,1,length(Str1)-3→Str1
I'm fairly new to Ruby (coming from C#), so I'm wondering why this is valid:
x = 2
x #why is this valid?
Does ruby interpret it as x.inspect or something internally?
I believe that Ruby follows the Lisp where expressions return their own value, and, in particular, some expressions are self-evaluating. As a result, return is actually unnecessary in Ruby.
Why is a variable a valid statement?
Because it isn't a statement, it's an expression. There are no statements in Ruby, everything is an expression.
Does ruby interpret it as x.inspect or something internally?
No. x is interpreted as x, nothing else.
In a REPL, like IRb or Pry, the REPL may or may not call some methods on the object which is the result of evaluating x in order to display some human-readable text representation of the object, but that is a) a feature of the REPL, not Ruby and b) applies to all expressions, not just local variable dereferences.
It is valid because x is a defined variable, i.e., it refers to an object. It doesn't interpret it as x.inspect or anything else. x is x (actually 2).
this is my first post here. I started using Ruby just 2 days ago and think it is an amazing language, however I have been getting stuck. My problem is I am wanting to calculate the event horizon of a black hole given an input defined in the code as "m" This will then be put into a calculation and the size then printed out to the screen. I did need it to be in binary and thats where I am having the issue.
Here is my code so far.
#Event Horizon Calculation Program
G = 6.67*10**-11
m = 20
C = 200000
R = G*m/(C**2)
puts "Here is the result in Binary."
R.to_i(2)
puts R
Now I do realise that the number are not accurate, that dosen't matter at the moment. I just need the function to work.
Thankyou,
Ross.
Your post is not even in a format of asking a question, but guessing from what you wrote, it seems that you are asking how to change your code so that it accepts an input to m and outputs the result. My answer is based on this assumption.
In order to take an input, use the 'gets' method. So, you may want to replace your 'm = 20' line with:
m = gets.to_f
'gets' accepts an input as a string, so you need to convert it to a numeric. to_f changes a string into a float. You can use to_i instead if you want an integer.
You have a line 'R.to_i(2)', and it seems like you want to output this, but you have two problems here. First of all, whatever that creates, it is only creating something in that position, and does not change the value of R, so, in effect, it actually does nothing. Second, ruby can accept numerals in source code written in different bases such decimal, binary, hex, etc., but it only has one internal representation, and you cannot output a numeral in binary. For your purpose, you need to convert it to a string that corresponds to a binary expression. For that, use the 'to_s' method. In fact, the 'to_i' method does not take an argument. Delete your line 'R.to_i(s)', and replace the line 'puts R' with:
puts R.to_s(2)