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 4 years ago.
Improve this question
Im trying to code in python a function that reacts to a input
example
Action = input("Input:")
function commands():
if action == Commands:
print("Commands so far")
but I don't know how to get the function to work.
any help?
Is this what you are asking for?
def react(action):
if action == 'act':
print('act given')
inp = input("Type action: ")
react(inp)
Related
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
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.
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
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
When I run this easy program and want to exit it, I need to type exit 2 times (at line 7).
def registration():
def username_registration():
entering_username = True
while entering_username:
print("Type exit to leave the registration.")
usernam_from_registration = input("Enter username: ")
if usernam_from_registration == "exit":
break
lenght_usernam_from_registration = len(usernam_from_registration)
if lenght_usernam_from_registration > 15:
print("too long")
else:
return usernam_from_registration
username_registration()
print(username_registration())
registration()
Why is this and how can I make it so I only need to write it one time?
This is because you are calling the username_registration() function twice.
username_registration()
print(username_registration())
The first time you call it, nothing happens because you are not doing anything with the result.
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 4 years ago.
Improve this question
This simply a concept question. In a chain of if statements what would happen in the following chain? if-elif-if. I am not sure how the code will flow with the if statement after the elif.
If there is an if statement after an elif, this means this a different block of if statements. For example, if I do
yes = "yes"
no = "no"
if yes == "yes":
print("yee")
elif no == "no":
print(":(")
It will only print "yee" as after it stopped after the first one worked. However, if we do this:
if yes == "yes":
print("yee")
if no == "no":
print(":(")
Both will print as they are two different statements with no correspondence to each other.