Python keeps saying invalid syntax - syntax

I'm currently trying to correct a code for a class and I when I try to the run the module it keeps saying invalid syntax. Please Help!
This is the code.
print (myName + ',thank you for taking this quiz. Your score was ' + myScore 'out of 3. ')

myName = "Jay"
myScore = 1
print ('%s thank you for taking this quiz. Your score was out %d of 3. ') %(myName, myScore)
This is a better way do it. The reason your code doesn't work is because you are missing a '+' after myScore.
print (myName + ',thank you for taking this quiz. Your score was ' + myScore + 'out of 3. ')
That is the corrected version.

Think deeper, your thought should think what syntax error you just made, not focus on a missing of a '+'

Related

How do I comment out a block of code in Stan?

This feels like a duplicate question, but I can't find it anywhere here or elsewhere!
So, in R, you can comment out a block of code with Ctrl + Shift + C/Cmd + Shift + C. Does anyone know how to perform the equivalent action in Stan? All I know is that Stan uses // in place of R's #. I've tried Ctrl + Shift + //Cmd + Shift + /, to no avail.
Thanks so much for any help! :)
You can create a comment within your Stan code by using the following:
/* this is a comment block
as opposed to a single
line block */
Please see, https://mc-stan.org/docs/2_18/reference-manual/bracketed-comments.html

Error in my ruby code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can't get this code to work. Can anyone help me by pointing out the error in this code so that I can understand where I made mistakes?
STDOUT.print 'Do you wish to input another length in meters? '
more = STDIN.getString
more = STDIN.getint( );
more = more.toUpper( )
while(more[1] = 'Y')
STDOUT.puts 'Enter length in meters: '
gets(meter)
f = meter * 3.28084
feet = f.toInt
inches = (12.0 * (feet - f)).to_i
print 'The length is '
if feet = 1
STDIN.print feet + 'foot ';
else
STDOUT.print feet + 'feet '
if inches = 1
STDOUT.print inches + ' inch.\n'
else if (inches < 1)
STDOUT inches + ' inches.\n'
else
STDOUT.print '.\n'
STDOUT.print 'Do you wish to input another length in meters: '
more = STDIN.getint
end
Where do I start?
In Ruby, you need to terminate your blocks with end. You did it for while, but not for if.
Ruby uses elsif; you can't write else if.
Ruby does not have toInt; it's called to_i, as you use in the very next line
gets(meter) is an error; you need to say meter = gets
STDIN does not have getString, it has gets. It also doesn't have getint, you need to write gets.to_i.
toUpper does not exist, use upcase, as in more = more.upcase. You can also use the more readable and more efficient more.upcase!.
In if and while, you have assignment =, where you presumably want to have comparison ==.
more[1] is the second character of more; the first being more[0].
more = ... is being called twice in a row. That means the first value you input will be discarded without effect.
STDIN.print is an obvious mistake for STDOUT.print.
You can use puts "..." instead of print "...\n".
STDIN and STDOUT are redundant when you are using gets, print and others; STDIN.gets is identical to gets, STDOUT.print is identical to print.
STDOUT inches + ' inches.\n' is an obvious mistake, since STDOUT is not a function.
'.\n' contains three characters: a period, a backslash and a letter. The double-quoted ".\n" contains two: a period and a newline.
Ruby does not typically use ;, and it does not usually use empty parentheses for calling 0-parameter functions. These are just stylistic errors, and won't impact runtime.
There may or may not be more.

Interpolation with conditionals

I want to insert data into a string via interpolation. I want to check if #call.transferred_from is nil, and if so, output #call.transfer_from_other; else output #call.transferred_from.try(:facility_name) along with #call.transferred_from.try(:facility_address).
Here is my code example:
"#{if #call.transferred_from.nil? #call.transfer_from_other else #call.transferred_from.try(:facility_name) #call.transferred_from.try(:facility_address) end}"
Doing this gives me the following error:
syntax error, unexpected keyword_else, expecting keyword_then or ';' or '\n'
I'm not sure where to go. Any help would be appreciated.
Update: 08/04/14
I moved the conditional into a private controller method as follows:
def transfer_from_address
if #call.transferred_from.nil?
#call.transfer_from_other
else
#call.transferred_from.try(:facility_name) + ' ' + #call.transferred_from.try(:facility_address)
end
end
Then I call the following using string interpolation.
#{transfer_from_address}
This seems to work, but I'm not sure that it's proper Ruby.
I know this is not really answering your question, but I'd caution about putting this much logic in an interpolation. While its totally doable, it makes your code very hard to understand.
The fundamental issue I see with your particular issue is you're trying to return 2 things somehow, yet you're just putting both of them next to eachother which is not valid ruby.
Assuming this is in an interpolation you'd want to somehow return them together ..
#{
#call.transferred_from.nil? ?
#call.transfer_from_other :
#call.transferred_from.try(:facility_name) + ' ' + #call.transferred_from.try(:facility_address)
}
I'd really suggest you move this into a variable or a method tho .. and just reference it in the interpolation.
This could look something like:
facility_name_and_address = #call.transferred_from.nil? ? #call.transfer_from_other : #call.transferred_from.try(:facility_name) + ' ' + #call.transferred_from.try(:facility_address)
{
:body => facility_name_and_address
}
If I understand what you are trying to do, I would suggest adding a method to #call which does the job:
class Call
def transfer_text
return transfer_from_other if transferred_from.nil?
"#{transferred_from.try(:facility_name)} #{transferred_from.try(:facility_address)}"
end
end
Then simply calling #call.transfer_text should provide the needed text.
If you want to be more sophisticated, and you don't want trailing white-space in case facility_name or facility_address are nil, you can create a list of them, and join them with white space:
[transferred_from.try(:facility_name), transferred_from.try(:facility_address)].compact.join(' ')
This will make sure spaces will be only between to non-nil elements. If both are nil, and empty string will be the result (rather than a space), and if one is nil, it won't have a leading/trailing space.
why not using
:body => "#{#call.transferred_from.nil? ? #call.transfer_from_other : #call.transferred_from.try(:facility_name) #call.transferred_from.try(:facility_address)"
but anyway I would not use this compact syntax for better maintainability
You just need to put either a semicolon or then right after the condition.
if #call.transferred_from.nil?; #call.transfer_from_other ...
But in your case, there is not much point in putting the entire condition inside a string interpolation. It is better to do the condition outside the string.
By the way, if you fix your first error, then you might encounter the next error:
#call.transferred_from.try(:facility_name) #call.transferred_from.try(:facility_address)
To fix that as well, I think you should do
#call.transferred_from.instance_eval{|e| e.nil? ?
#call.transfer_from_other.to_s :
"#{e.try(:facility_name)} #{e.try(:facility_address)}"
}

struggling with my pseudocode

I'm about to build a program written in pseudocode. I've already done most of the work , but I'm stuck on the code and I don't know what to do exactly. im a begginer and not everything is clear to me ...in one of the tasks i have to do , i have to make the program ask for the players name , which will be stored as a string then the program has to check if it exceeds the limit between 2/20 characters and inform the user if the input is wrong . i have researched and tried to figure out how i might be able to fix my code but i have a really short amount of time left and coudn't find anything regarding my problem :/ . this is the code ive done for this specific task. i know its wrong but i just dont know how to fix it . any help with be much appreciated . Thanks in advance :)
pseudocode:
// Getting user's name
valid = false
loop until valid is equal to true
Output" please enter your name "
Input playName
If (playName is => 1)AND(=<20)then
Valid = true
Otherwise
output "name exceeds the character limit"
I'm not sure what the syntax of your pseudo code is but :
assuming tabulation has meaning, you may have forgot to indent some lines to include them in the loop
'valid' is first declared with a lower case first letter so you may continue referencing it same way in line "Valid = true" -> "valid = true"
In the 'If' you want to test the lenght of the String and not compare the string to an int so maybe call a function length(String) that would return the length of the string or access a string.length attribute (as you wish in pseudo code)
You want the playName to be superior or equal to 2 "length(playName) >= 2" and inferior or equal to 20 "length(playname) <= 20"
The commonly used keyword meaning Otherwise is 'Else' as in
IF (Condition) THEN (code) ELSE (code)
I may modify you code like this :
// Getting user's name
valid = false
loop until valid is equal to true
Output" please enter your name "
Input playName
If (length(playName) >= 2) AND (length(playName) <= 20)
Then
valid = true
Else
output "name exceeds the character limit"

Why's poignant guide to Ruby chapter 5.7 . The WishScanner module

I was reading Why's poignant guide to ruby and came upon this module on chapter 5:
require 'endertromb'
module WishScanner
def scan_for_a_wish
wish = self.read.detect do |thought|
thought.index( 'wish: ' ) == 0
end
wish.gsub( 'wish: ', '' )
end
end
I have been trying to figure out how it works but what is confusing me is how thought.index( 'wish: ' ) == 0 and wish.gsub( 'wish: ', '' ) work. According to the author the purpose of this method is to "only pick up a wish if it starts with the word wish and a colon and a space. That way the planet doesn’t fill up with every less-than-ten-letter word that appears in people’s heads.”
But the way I understand it, thought.index( 'wish: ' ) == 0 will check if thought starts with 'wish: '. However i dont understand wish.gsub( 'wish: ', '' ) in the way I see it it seems to just substitute wish with an empty string.
Can any one offer any explanation or further insight into it?
Thank you in advance for any help.
wish.gsub( 'wish: ', '' ) in the way I see it it seems to just substitute wish with an empty string
Exactly! It replaces the string "wish: " in a string like "wish: A pony!" with "".
I.e. it turns "wish: A pony!" into "A pony!".

Resources