why does result() take no arguments? [closed] - python-3.x

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 2 years ago.
Improve this question
class result:
def _init_(self,phy,chem,math):
self.phy=phy
self.chem=chem
self.math=math
def printavg(self):
print(f"average={(self.phy+self.chem+self.math)/3}")
rollone=result(86,95,85)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: result() takes no arguments
getting the same error again and I used init constructor in the right way
is anyone to explain is most welcome and benevolent of you?

Your declaration of init is wrong, it is __init__ and not _init_, ie, with double underscores :
class result:
def __init__(self,phy,chem,math):
self.phy=phy
self.chem=chem
self.math=math
def printavg(self):
print(f"average={(self.phy+self.chem+self.math)/3}")

Related

SyntaxError: invalid syntax, I don't know how to fix this code. What's wrong with it? And how to solve it? [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 4 months ago.
Improve this question
The part of the code that produced the error
def modinverse(a,26):
for x in range(0,26):
if(((a%26)*(b%26))%26 == 1):
return x
return -1
print(modinverse(a,26))
Here is the error I got
File "main.py", line 51
def modinverse(a,26):
^
SyntaxError: invalid syntax
I checked,it looks good. The spellings are fine, brackets and stuff are closed, I don't know what to do. I can't see what's gone wrong.
I ran this on replit.
Maybe def modinverse(a,26) should be something like def modinverse(a,b) or def modinverse(a,b=26):?
In the first case you are declaring a parameter with the name 26.

TypeError: 'str' object is not callable (When printing an integer and words) [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 2 years ago.
Improve this question
#stage 2 dice rolling
#P1D1 means player ones dice one value and so on with P1D2
import random
print("player ones turn")
P1D1 = random.randint (1,6)
print ("your number is "(P1D1))
(this is in python 3.6.3)
when i run this piece of code it returns str object not callable
but when i run
print(P1D1) it seems to print the number without issue
anyone able to help thanks in advance
The error is caused by your syntax on the last line. By putting the brackets right after a string it is like trying to call the string as a function, just like how putting brackets around print, it calls it. Try this.
print ("your number is ", P1D1)

invalid token with hex() [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 3 years ago.
Improve this question
I have a set of binary number, some of them start with 0, for example:
000000000001
When I use hex(000000000001) I get the following error:
>>> hex(0000000000000001)
File "<stdin>", line 1
hex(0000000000000001)
^
SyntaxError: invalid token
But I dont have with :
>>> hex(0000000000000000)
'0x0'
How to pass some digit if they start with zero?
You have to do the following steps:
string_of_bits = '0b' + '000000000001'
hex(int(string_of_bits,2))

Python 3.5.2 .format() [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 6 years ago.
Improve this question
I just started to learn python and I cant figure out how to use .format, I get a syntax problem every time I try it. Here's the code.
my_name = input("What is your name:")
my_age = input("What is your age:")
print ("So you are {name} and you are{age}")format(name=my_name,age=my_age)
Can someone tell me what I did wrong?
format is a method of str so you should call that from your string, then pass the result of that into print.
print ("So you are {name} and you are{age}".format(name=my_name,age=my_age))

Py3k identical code executing differently in dynamic shell and from file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
The code I'm using is:
import time
print(time.strftime("%H:%M:%S"))
In the dynamic shell, this (as you would expect) outputs a formatted string, e.g. 03:21:35
When executing the exact same code from a file, it throws the following error:
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(time.strfime("%H:%M:%S"))
AttributeError: 'module' object has no attribute 'strfime'
Anybody got any idea as to why this might be happening, and more importantly, how to fix it?
You've got a typo:
print(time.strfime("%H:%M:%S"))
should be
print(time.strftime("%H:%M:%S"))

Resources