incomplete python code not executing [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am an absolute beginner and learning python, I wrote a code but it's incomplete and I have no clue about the missing part. Can anyone help please?
def my_age_months(x):
a = my_age_months(36)
return (x*12)
print("a:")
print(a)
if a > 400:
return "you are not young"
else:
return "you are young"

What you wanna look out for is indentation.
Python code is all about correct indentation
and white space is important so structure your code nicely
as python is about readable and neat code.
Best of Luck, Happy Coding :)
def my_age_months(x):
return (x*12)
a = my_age_months(36)
print("a:")
print(a)
if a > 400:
return "you are not young"
else:
return "you are young"

You put
a = my_age_months(36)
in the def function, which will make it not work.
I presume you want is this:
def my_age_months(x):
return (x*12)
print("a:")
print(a)
if a > 400:
return "you are not young"
else:
return "you are young"
my_age_months(36)

Related

Why is my if statement printing my else without error? [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 1 year ago.
Improve this question
I have tried everything I could before asking this question... I am making a little text adventure game, really simple and the only code in it is basically just input(). But when they do something wrong, I want to add an else: and print("Thats not valid!") and go back to example = input(), but my else statement is printing and re-doing input with a correct statement, so its still executing else without any error, can anyone help? I don't know why its doing this and all the examples I have seen this is valid code, and my else statement just executes without checking if it was wrong...
```python
if prompt1 == "y":
print("Lets start")
else:
prompt1 = input()
It then executes input again...
If that's the exact code the indention is wrong.
The if and else of same condition should have same indention:
if condition:
do this
else:
do this

How do i run this program i am new to oops [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 1 year ago.
Improve this question
Can anyone help me out with this program please, I am beginner
The problem seems to be you forgot parentheses after creating enemy1.
class Enemy:
life = 3
def attack(self):
print('ouch')
self.life -= 1 # notice the - signs comes before the equal sign
def checklife(self):
if self.life <= 0:
print('i am dead')
else:
print(str(self.life) + ' life left') # notice where ' ` ` life left' comes
enemy1 = Enemy() # added parantheses after `Enemy`
enemy1.attack()
enemy1.checklife()
And remember, it is much better to post the code as text rather than a picture.

Somebody help me understand this ctypes python code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
hotkey_ = enable_shortcut # Check to break the while loop
def proper_fctn():
if hotkey_:
if not user32.RegisterHotKey(None, shortcut_id, modifier, vk):
pass
try:
msg = wintypes.MSG()
while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if not hotkey_:
break
fctn_to_run()
user32.TranslateMessage(byref(msg))
user32.DispatchMessageA(byref(msg))
except:
pass
If somebody could help me understand the lines, so I could understand the process better.
These are Win32 APIs. All of them are well-documented in Microsoft's MSDN documentation. You've basically written a Windows application with a standard main loop.
Google takes you straight to their docs.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage

How to find/replace python statement with multiple lines and keep the correct indentation? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'd like to find one statement in multiple python source files and replace all of those with multiple statements, but I don't know how to retain the correct indentations. Here is a simple example:
print("Hello World!")
if i == 0:
print("Hello World!")
if j == 0:
print("Hello World!")
I want to replace all the print("Hello World!"); with multiple lines:
print("Hello")
print("World")
print("!")
AND keep the correct indentation, so here is the expected result:
print("Hello")
print("World")
print("!")
if i == 0:
print("Hello")
print("World")
print("!")
if j == 0:
print("Hello")
print("World")
print("!")
This is just a simple example, not the exact same find/replace problem I'm having. I just want to know how to do this kind of replacement generally. Is there any method to achieve this easily? Any editor/IDE can help? Thank you.
You can use Visual Studio Code or any other editor with Regular Expression Find-Replace
Find ([ \t]*)print\("Hello World!"\)
Replace $1print("Hello")\n$1print(" World")\n$1print("!")
Some editors (Notepad++, ...) use \1 to reference a capture group
Replace \1print("Hello")\n\1print(" World")\n\1print("!")

Invalid syntax on if else statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing a simple text-based game and I've run into an error here.
def lamp():
print "You pick up the lamp and examine it."
print "It looks like an ordinary gas lamp."
print "What do you do?"
lamp_action = raw_input("> ")
if "rub" in lamp_action:
rub()
elif "break" or "smash" in lamp_action:
print "The lamp shatters into pieces."
dead("The room disappears and you are lost in the void.")
else:
lamp()
If I comment out the elif part, Python gives an invalid syntax error on else. If I leave the elif part in, the program will run without an error, but even typing something random like "aaaaaa" will follow the elif action.
I also doesn't work if I replace the else section with something like this:
else:
print "That's not a good idea."
lamp()
or like this:
else:
dead("That's not a good idea.")
Where dead is:
def dead(why):
print "%s Game over." % why
exit(0)
What did I miss?
"break" or "smash" in lamp_action is interpreted by Python as testing "break", then testing "smash" in lamp_action. Since "break" is a nonempty string, it is always interpreted as "true", so the elif is always taken.
The correct form is
elif lamp_action in ('break', 'smash'):
i.e. testing if the action is in a list of possibilities.

Categories