Can eval function be used in concatenating a string and an integer? - python-3.7

I need to know if i am providing a=3 and b=kite then the output should 3kite but it is showing an error, plz help me through this...
a=eval(input("enter a number:"))
b=eval(input("enter second data:"))
c=(a+b)
print("the result is:",c)

Related

How to convert a number into a string in Maple?

I want to have something like this code from Python
num=3
res=str(num)
but in Maple. I couldn't find any appropriate constructors for this. Are there any?
num:=3:
convert(num,string);
"3"
sprintf("%a",num);
"3"
The best way is to use convert as already exists in #acer's answer. Just to name one more possibility here is another way.
num := 3:
res := cat( "", num );
You will get "3" for res of type string. What cat here does is concatenating 3 to the empty string "", and when there exists at least one string in the arguments of cat, the output becomes a string. You can even have something like sqrt(2) instead of 3 in num, in that case res becomes this string; "2^(1/2)". But sometimes it may give you a non-string object, for example if the number in num is of the form RootOf. See the help page to read more.

SSRS nested IIF/CStr explanation

Could someone let me know if the IIF statement below means output any value that starts with a 4 please?
=IIF(LEFT(CStr(Fields!CLOCK_NUMBER.Value),1)="4",Fields!JOB_NO.Value, "")
The short answer is yes.
Starting from the middle and working outwards this expression is doing the following..
Get the value of the field CLOCK_NUMBER
Convert this to a string (the CSTR function)
Take the 1st character (LEFT function with 1 as the seconds parameter)
If the equals "4" return the Value that is in JOB_NO
Otherwise return an empty string
If this is not working for some reason, try converting the job_no to a string before returning, that way you ensure you always return a string (in case JOB_NO is numeric). You can simply wrap the job_no in a CSTR like this CSTR(Fields!JOB_NO.Value)
Translates to..."try to" convert the field CLOCK_NUMBERS's native value to a string and take the LEFT(1) most significant digit(s) and if that value is "4" then return the JOB_NO Fields's value. else return empty string.
So, it is, if the first digit is 4 then return JOB_NO.

How to Determine if a given User input is a Float, String, or a Integer in Ruby

The is_a? method doesn't work; I have tried it and it apparently it checks if the value is derived from an object or something.
I tried something like this:
printf "What is the Regular Price of the book that you are purchasing?"
regular_price=gets.chomp
if regular_price.to_i.to_s == regular_price
print "Thank You #{regular_price}"
break
else
print "Please enter your price as a number"
end
Can someone explain to me more what .to_i and .to_s do? I just thought they convert the user input to a string, or a Numerical Value. I actually don't know how to check input to see if what he put in was a float, a String, or a decimal.
I just keep getting Syntax errors. I just want to know how to check for any of the 3 values and handle them accordingly.
There's a lot to your question so I recommend that you read How do I ask a good question? to help you get answers in the future. I'll go through each of your questions and try to provide answers to point you in the right direction.
The is_a? method works by accepting a class as a parameter and returning boolean. For example:
'foo'.is_a?(String)
=> true
1234.is_a?(Integer)
=> true
'foo'.is_a?(Integer)
=> false
1234.is_a?(String)
=> false
1.234.is_a?(Float)
=> true
The .to_i method is defined on the String class and will convert a string to an Integer. If there is no valid integer at the start of the string then it will return 0. For example:
"12345".to_i #=> 12345
"99 red balloons".to_i #=> 99
"0a".to_i #=> 0
"hello".to_i #=> 0
The .to_s method on the Integer class will return the string representation of the Integer. For example:
1234.to_s
=> '1234'
The same is true of Float:
1.234.to_s
=> '1.234'
Now let's take a look at your code. When I run it I get SyntaxError: (eval):4: Can't escape from eval with break which is happening because break has nothing to break out of; it isn't used to break out of an if statement but is instead used to break out of a block. For example:
if true
break
end
raises an error. But this does not:
loop do
if true
break
end
end
The reason is that calling break says "break out of the enclosing block," which in this case is the loop do ... end block. In the previous example there was no block enclosing the if statement. You can find more detailed explanations of the behavior of break elsewhere on stackoverflow.
Your final question was "I just want to know how to check for any of the 3 values and handle them accordingly." This answer explains how to do that but the code example is written in a way that's hard to decipher, so I've rewritten it below in an expanded form to make it clear what's happening:
regular_price = gets.chomp
begin
puts Integer(regular_price)
rescue
begin
puts Float(regular_price)
rescue
puts 'please enter your price as an Integer or Float'
end
end
What this code does is first it attempts to convert the string regular_price to an Integer. This raises an exception if it can't be converted. For example:
Integer('1234')
=> 1234
Integer('1.234')
ArgumentError: invalid value for Integer(): "1.234"
Integer('foo')
ArgumentError: invalid value for Integer(): "foo"
If an exception is raised then the rescue line stops the exception from being raised and instead continues executing on the next line. In this case, we're saying "if you can't convert to Integer then rescue and try to convert to Float." This works the same way as converting to Integer:
Float('1.234')
=> 1.234
Float('foo')
ArgumentError: invalid value for Float(): "foo"
Finally, we say "if you can't convert to Float then rescue and show an error message."
I hope this helps and answers your questions.

CATIA macro (Input box not working)

Help My Balloon finding macro is not working with input box, it works only when i manually add the balloon number.. please tell me what i m missing ...Ferdo m expecting you
Language="VBSCRIPT"
Sub CATMain()
Set drawingDocument1 = CATIA.ActiveDocument
Set selection1 = drawingDocument1.Selection
result = InputBox("Ballon Number ?", "Title") 'The variable is assigned the value entered in the InputBox
selection1.Search "CATDrwSearch.DrwBalloon.BalloonPartName_CAP= result ,all"
End Sub
I don't know what you are doing but the last line looks wrong. I don't know what the docs are for your function but you are passing the string result rather than the value of the variable result because it is in quotes. Assuming your line is otherwise right ...
selection1.Search "CATDrwSearch.DrwBalloon.BalloonPartName_CAP= " & result & ",all"

why strcat() doesn't return a string in Matlab?

I'm trying to access multiple files in a for loop, like this:
age = xlsread(strcat('Pipeline_BO_2013_',names(2),'_CDBU.xlsx'), 'Data', 'H:I')
It returns an error the filename must be string. So I did following test:
filename = strcat('Pipeline_BO_2013_',names(2),'_CDBU.xlsx')
filename =
'Pipeline_BO_2013_0107_CDBU.xlsx'
isstr(filename)
ans =
0
This is so weird. Could any one help me out? Thank you so much.
It looks like names is a cellstr and not a char array. If so, indexing in to it with parentheses like names(2) will return a 1-long cellstr array, not a char array. And when strcat is called with any of its arguments as a cellstr, it returns a cellstr. Then xlsread errors because it wants a char, not a cellstr.
Instead of just calling isstr or ischar on filename, do class(filename) and it'll tell you what it is.
Another clue is that filename is displayed with quotes. This is how cellstrs are displayed. If it were a char array, it would be displayed without quotes.
If this is the case, and names is a cellstr, you need to use {} indexing to "pop out" the cell contents.
filename = strcat('Pipeline_BO_2013_',names{2},'_CDBU.xlsx')
Or you can use sprintf, which you may find more readable, and will be more flexible once you start interpolating multiple arguments of different types.
filename = sprintf('Pipeline_BO_2013_%s_CDBU.xlsx', names{2})
% An example of more flexibility:
year = 2013;
filename = sprintf('Pipeline_BO_%04d_%s_CDBU.xlsx', year, names{2})