Is this code wrong? Im getting an invalid syntax error. Im a beginner coder in high school btw - python-2.x

I'm coding a program that based on your mood, it will give you quotes. Sort of like a motivational quote app. It isn't finished yet and I'm aware the lists aren't programmed yet.
import random
UserFeeling_str = raw_input("How are you feeling today?")
if UserFeeling_str == "upset
if UserFeeling_str == "sad":
if UserFeeling_str == "bad":
if UserFeeling_str == "not great":
print(random.choice(upset.list))
if UserFeeling_str == "happy":
if UserFeeling_str == "good":
if UserFeeling_str == "okay":
if UserFeeling_str == "ok":
print(random.choice(motiv.list))
and when I run the code:
File "/home/ubuntu/workspace/ex50/app.py", line 9
UserFeeling_str = raw_input("How are you feeling today?")
^
SyntaxError: invalid syntax
Process exited with code: 0

You're missing ": on your first if sentence.
Should be
if UserFeeling_str == "upset":
Also, don't forget the indent on every if block:
If something:
If something_else:
#actions

Notice the type of error that it is giving you is a "syntax error" those are types of errors where usually there is something that is missing or added in terms of a character or two that is misplaced, ie, the syntax is bad for the interpreter.
Just from what I see:
if UserFeeling_str == "upset
needs to be
if UserFeeling_str == "upset":
notice the quotation mark and colon.

Related

Writing program for guessing a number in Small basic but the program won't run

this is the code I'm using
Sub btnguess_Click
randNum = Math.GetRandomNumber(10)
FCControls.GetText(txtCels)
If txtCels = randNum Then
lblanswer = "That's correct the Madame would like to hire you!"
ElseIf txtCels <1 or >10 Then
lblanswer = "Please enter number between 1 and 10"
Elseif txtCels <> randNum Then
lblanswer = "That's incorrect the Madame asks you to leave."
EndIf
endsub
error that I'm getting : 45,24:Expected a condition here.
I'm not sure what I'm missing here
The problem is at ElseIf txtCels <1 or >10 Then
When specifying a statement, you can't leave out a condition even if previously mentioned.
You'll have to re-write txtCels before the >10, i.e.
ElseIf txtCels <1 or txtCels >10 Then
Without this, you're essentially saying 'greater than 10' by itself, which makes no sense.

How to return to beginning of program from inside of if statement?

I'm practicing some basic coding, I'm running a simple math program running in the terminal on Visual Studio Code.
How do I create an option to return to the beginning of the program, or exit the program after getting caught in an if statement?
Example:
#beginning of program
user_input=input('Please select "this" or "that": ')
findings=user_input
If findings == this:
print(this)
# How can I redirect back to first user input question, instead
# of just ending here?
if findings == that:
print (that)
# Again, How do I redirect back to first user input, instead of
# the program ending here?
# Can I setup a Play_again here with options to return to user_input,
# or exit program? And then have all other If statements
# redirect here after completion? How would I do that? with
# another If? or with a for loop?
#end program
You can try wrapping the whole program in a while loop like this:
while(True):
user_input=input('Please select "this" or "that": ')
this = 'foo'
if user_input == this:
print(this)
continue
if user_input == this:
print(this)
continue
Unfortunately, that 'another technique' I thought of using didn't work.
Here's the code (the first example modified):
import sys
def main():
# Lots of setup code here.
def start_over():
return #Do nothing and continue from the next line
condition = 1==1 #Just a sample condition, replace it with "check(condition)".
float_condition = condition
def play(*just_define:bool):
if not just_define:
play_again = input('play again? "y" or "n"')
if play_again == 'y':
start_over() #Jump to the beginning of the prohram.
if play_again == 'n':
sys.exit() #Exit the program
while True:
if float_condition == True:
# print(float_condition)
play() #skip to play_again from here?
if float_condition == False:
#print(float_condition)
play() #skip to play_again from here?
#I removed the extra "main()" which was here because it'd cause an infinite loop-like error.
main()
Output:
play again? "y" or "n"y
play again? "y" or "n"n
Process finished with exit code 0
The * in the play(*just_define:bool) function makes the just_define parameter optional. Use the parameter if you want to only tell Python to search for this function, so it doesn't throw a ReferenceError and not execute anything that's after the line if not just_define:. How to call like so: play(just_define=True).
I've used nested functions. I've defined play so that you can call it from other places in your code.
Thanks to #Hack3r - I was finally able to choose to return back to the beginning of the program or exit out. But it resulted in a new issue. Now my print(results) are printing 4 or 5 times...
Here is the actual code I built and am working with:
def main():
math_Options=['Addition +','Subtraction -','Multiplication *','Division /']
math_func=['+','-','*','/']
for options in math_Options:
print(options)
print('Lets do some Math! What math function would you like to use? ')
while True:
my_Math_Function = input('Please make your choice from list above using the function symbol: ')
my_Number1=input('Please select your first number: ')
x=float(my_Number1)
print('Your 1st # is: ', x)
my_Number2=input('Please select your Second Number: ')
y=float(my_Number2)
print('Your 2nd # is: ', y)
z=float()
print('')
for Math_function in math_func:
if my_Math_Function == math_func[0]:
z=x+y
if my_Math_Function == math_func[1]:
z=x-y
if my_Math_Function == math_func[2]:
z=x*y
if my_Math_Function == math_func[3]:
z=x/y
if (z % 2) == 0 and z>0:
print(z, ' Is an EVEN POSITIVE Number')
if (z % 2) == 1 and z>0:
print(z, ' IS a ODD POSTIVE Number')
if (z % 2) == 0 and z<0:
print(z, ' Is an EVEN NEGATIVE Number')
if (z % 2) ==1 and z<0:
print(z, ' IS a ODD NEGATIVE Number')
if z==0:
print(z, 'Is is Equal to Zero')
print('')
play_again=input('Would you like to play again? "y" or "n" ')
if play_again == 'y':
continue
if play_again == 'n':
break
main()
main()

'<=' and '>=' operators do not work

To check if the the installer is installed, I did:
installer status |grep Version| cut -c12-13
The ouput [sic] says:
installer not found
But if it were installed, it would say 11 or 10 (any numeric). If the output is <=10 || >=11, then it would say not installed, and would proceed with the installation. In the library, it gives:
def get_installer_linux_version
begin
cmd = Mixlib::ShellOut.new('installer status |grep Version| cut -c12-13')
cmd.run_command
rescue Errno::ENOENT => e
return '0.0'
end
return 'Version 10' if cmd.stdout.include? '10'
return 'Version 11' if cmd.stdout.include? '11'
end
In the install recipe is:
if get_installer_linux_version.to_i <= 10 || get_installer_linux_version.to_i >= 11
log 'installer is installed'
else
log 'installer is not installed so procceding with the installation'
There are numerous things in your code that could be considered flawed, but for the sake of your question, I will focus on that.
get_installer_linux_version returns a few possible values. Either the string "version 10", "version 11", "0.0", or nil. You call this function, then call to to_i on it.
Maybe this will help illistrate it:
"version 10".to_i
#=> 0
I am going to guess that is not the intended behavior. No matter what happens in your get_installer_linux_version, the returned string will always be 0 after you call to_i on it. You then make condition that <= 10, which 0 is still less than 10, and it logs "installer is installed".
I would also venture to guess that using include? 'XX' is going to end up causing you problems when dealing with version numbers, but that is another question.
To further illustrate, let me write your code as it will always, always, always be as you have it written, and see if something looks off:
if 0 <= 10 || 0 >= 11
log 'installer is installed'
else
log 'installer is not installed so procceding with the installation'
What do you expect the result to be?? Although 0 is not ever going to be greater than 11, it will always be less than 10, so your result is always the same.
Just going to put this here, as it may be relevant the more I look at your sample.
>=
Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
<=
Checks if the value of left operand is less than or equal to the value
of right operand, if yes then condition becomes true.

ValueError: not enough values to pack (expected 2, got 1) syntax error

Hello I am taking an example from learn python the hard way and working to understand python. I have already caught one error in the book and realized you need () to print out strings. So maybe there could be some more mistakes in the syntax. when I ran this file I received an error that said
ValueError: not enough values to pack (expected 2, got 1) syntax error. And another general syntax error. So I can't set prompt to raw_input.
rom sys import argv
script, user_name = argv
prompt = '>'
print ("Hi %s, I'm the $s script.") % user_name, script
print ("I'd like to ask you a few questions.")
print ("Do you like %s?") % user_name
likes = raw_input(prompt)
print ("Where do you live %s") % user_name
lives = raw_input(prompt)
print """
(Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice)
"""& (likes, lives, computer)
Is there a typo in the code example? $s instead of %s in the format string?
I suspect that your actual code has this:
print ("Hi %s, I'm the %s script.") % user_name, script
And you meant to write this:
print "Hi %s, I'm the %s script." % (user_name, script)
That is, print a single string which is the result of applying the format string to two arguments.

Check if button is pressed or not

I have a Raspberry Pi with a Siri Proxy that is controlling my garage door, the garage door has only one command for open and close. To check if the garage door is opened to not I bought a magnet switch and I builded a flout point button. I already tried
doorstate = `gpio read 5`.chomp #gives value 1 or 0, 1 is opened, 0 is closed
print doorstate
if doorstate == "1"
print "The garage door is already opened.\n"
elsif doorstate == "0"
print "OK, I'll open it for you!\n"
else
print "Error, please open it manually.\n"
end
Can someone please tell me how I can check fi the returned value or string from doorstate = 'gpio read 5' is equal to a string?
I'm guessing here that the result of 'gpio read 5' contains a line ending.
Try chomping it off:
doorstate = `gpio read 5`.chomp
To verify the class of doorstate, insert p doorstate.class at line 2.
You need to change your single quotes (') to backticks (`, the little thing with the tilde on your keyboard). That will execute the command. The rest of your code is fine.

Resources