How do i run this program i am new to oops [closed] - python

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.

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

Not instantly getting out a loop with break [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
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.

incomplete python code not executing [closed]

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)

Input for function [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 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)

Doing something after for loop finishes in same function? [closed]

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 8 years ago.
Improve this question
Is this possible? I'm doing an bukkit plugin now (in Python, yes :D), and I'm forced to do this within one function, so I can't separate it and call it later... For example, if I have loop that loops through players on server and adds everyone except one player, I want it to finish, and then teleport i.e. "Player1" to random player. At the moment, it teleports "Player1" to random player every time because of for loop... I'll give you just little of code, since It looks messy in preview due to many things that are not involved in problem and could be confusable to you... Here it is:
listica = []
for p1 in org.bukkit.Bukkit.getWorld(nextvalue).getPlayers():
if p1.getName() not in listica:
try:
listica.remove(event.getPlayer().getName())
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"% (bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
except ValueError:
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
if event.getPlayer().getLocation() != randomtargetreal.getLocation():
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"%(bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
What I want is:
run for loop:
when there is no more players to add a.k.a it finishes
do try loop
P.S. I can't do it in separate function.
Thanks in advance! :)
Do you mean:
def func(args):
for item in loop:
do something
try: # note indentation
something else

Categories