• Starting today August 7th, 2024, in order to post in the Married Couples, Courting Couples, or Singles forums, you will not be allowed to post if you have your Marital status designated as private. Announcements will be made in the respective forums as well but please note that if yours is currently listed as Private, you will need to submit a ticket in the Support Area to have yours changed.

  • CF has always been a site that welcomes people from different backgrounds and beliefs to participate in discussion and even debate. That is the nature of its ministry. In view of recent events emotions are running very high. We need to remind people of some basic principles in debating on this site. We need to be civil when we express differences in opinion. No personal attacks. Avoid you, your statements. Don't characterize an entire political party with comparisons to Fascism or Communism or other extreme movements that committed atrocities. CF is not the place for broad brush or blanket statements about groups and political parties. Put the broad brushes and blankets away when you come to CF, better yet, put them in the incinerator. Debate had no place for them. We need to remember that people that commit acts of violence represent themselves or a small extreme faction.
  • We hope the site problems here are now solved, however, if you still have any issues, please start a ticket in Contact Us

  • The rule regarding AI content has been updated. The rule now rules as follows:

    Be sure to credit AI when copying and pasting AI sources. Link to the site of the AI search, just like linking to an article.

Python help

charsan

Charismatic Episcopal Church
Jul 12, 2019
2,297
2,115
54
South California
✟62,421.00
Country
United States
Gender
Male
Faith
Christian
Marital Status
Married
Politics
US-Republican
Hi all,

If anyone knows Python I could use some help. I need to make a class that takes user input to add numbers to a list then sum the numbers so if the user enters 1, 2, and 3 the sum of 3 + 2 + 1 would be 6, than I have to reverse the list (I know there is a reverse function) and finally show the list as entered.

I can do this:

Converting the three-digit number to a string to get each individual character. You should use modulus and divide.

I can't:
print in any method of the class
Using input in any method of the class.

I am totally lost. :( If anyone can help I would so greatful
 

Occams Barber

Newbie
Site Supporter
Aug 8, 2012
6,493
7,693
77
Northern NSW
✟1,103,558.00
Country
Australia
Gender
Male
Faith
Atheist
Marital Status
Divorced
Hi all,

If anyone knows Python I could use some help. I need to make a class that takes user input to add numbers to a list then sum the numbers so if the user enters 1, 2, and 3 the sum of 3 + 2 + 1 would be 6, than I have to reverse the list (I know there is a reverse function) and finally show the list as entered.

I can do this:

Converting the three-digit number to a string to get each individual character. You should use modulus and divide.

I can't:
print in any method of the class
Using input in any method of the class.

I am totally lost. :( If anyone can help I would so greatful

I found this under 'Python Help' on Google

Don't thank me - You're welcome:)
images


OB
 
Last edited:
Upvote 0

Tanj

Redefined comfortable middle class
Mar 31, 2017
7,682
8,318
60
Australia
✟284,806.00
Country
Australia
Gender
Male
Faith
Atheist
Marital Status
Married
Hi all,

If anyone knows Python I could use some help. I need to make a class that takes user input to add numbers to a list then sum the numbers so if the user enters 1, 2, and 3 the sum of 3 + 2 + 1 would be 6, than I have to reverse the list (I know there is a reverse function) and finally show the list as entered.

I can do this:

Converting the three-digit number to a string to get each individual character. You should use modulus and divide.

I can't:
print in any method of the class
Using input in any method of the class.

I am totally lost. :( If anyone can help I would so greatful

I have 30 years of python programming experience, but I'm not sure what the problem is. What do you mean you can't print in any method of the class, or "using input in any method of the class"

Might help if you posted the code.
 
Upvote 0

charsan

Charismatic Episcopal Church
Jul 12, 2019
2,297
2,115
54
South California
✟62,421.00
Country
United States
Gender
Male
Faith
Christian
Marital Status
Married
Politics
US-Republican
I have 30 years of python programming experience, but I'm not sure what the problem is. What do you mean you can't print in any method of the class, or "using input in any method of the class"

Might help if you posted the code.

Well that was what the assignment said, that I can't have the user input inside the method, I have to have it for a lack of a better word in the main part of the program. Does that make sense?

Assignment 1. Console Application
For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify a three-digit whole number:

sumNums - This method takes as input a three digit int and returns the sum of the three numbers. For instance, 123 would return 6 because 3 + 2 + 1 is 6

reverseNums - This method takes as input a three digit whole number and returns the number in reverse as a String. For instance, 123 would return "321"

getList - This method takes as an argument a three digit whole number and returns the numbers as an int list. Each element of the list should contain one of the numbers

In the main method prompt for a three digit number to be input.

  1. Create an instance of the Lab1 class
  2. Call the sumNums method passing to it the whole number that was input into the Console window
  3. Call the reverseNums method passing to it the whole number that was input into the Console window.
  4. Call the getList method passing the whole number that was input at the Console window. Print the list out one number per line from the Main method using a for loop.
Note:

You can use a for loop to iterate through a list as follows:

myList = [2,4,6,8,10]

for nums in myList:
print(nums)

Your output should be the sum of the digits, the whole number in reverse order as a String, and the int as a list.

Your output should resemble the following:

download


Things to Review
  • for loops
  • counted loops with while
  • lists
  • Value producing methods
Things that will cost you points
  • Converting the three-digit number to a string to get each individual character. You should use modulus and divide.
  • print in any method of the class
  • Using input in any method of the class.

I posted the assignment now. It's been 19 years since I took python 1 and I don't understand classes. Now I even studied "Learn Python the hard way" and get everything but I don't understand classes. Like I need to make a list where I take input from the user so I think something like this, by the way I have two files:

File one - Lab1.py
class Lab1 :
def __init__(self, myList) :
self.setmyList = newList = []

File 2 - Assingment1.py
from Lab1 import Lab1

#here I am not sure if this is right
lis = newList(int(input("Enter a number: ")))

This does not look right, I think I bungled it typing here :)
 
Upvote 0

Tanj

Redefined comfortable middle class
Mar 31, 2017
7,682
8,318
60
Australia
✟284,806.00
Country
Australia
Gender
Male
Faith
Atheist
Marital Status
Married
Well that was what the assignment said, that I can't have the user input inside the method, I have to have it for a lack of a better word in the main part of the program. Does that make sense?



I posted the assignment now. It's been 19 years since I took python 1 and I don't understand classes. Now I even studied "Learn Python the hard way" and get everything but I don't understand classes. Like I need to make a list where I take input from the user so I think something like this, by the way I have two files:

File one - Lab1.py
class Lab1 :
def __init__(self, myList) :
self.setmyList = newList = []

File 2 - Assingment1.py
from Lab1 import Lab1

#here I am not sure if this is right
lis = newList(int(input("Enter a number: ")))

This does not look right, I think I bungled it typing here :)

Well, firstly might I say that's a really silly usage of classes...but that's not your fault. Anyway, I suspect from the instructions what they are saying is that the input and print stuff needs to be in the main function, not the class. In other words

DONT DO THIS:

class Lab1:
...def someMethod(self):
......input
......print



DO THIS

class Lab1:
...def __init__(self,x):
......self.rawData = int(x)

...def sumNums(self):
......Do some logic processing here
......return sumNumsResult

......

def main:
...x = input('Please enter the 3 digit number')
...myObj = Lab1(x)
...print(myObj.sumNums())
 
Upvote 0

Tanj

Redefined comfortable middle class
Mar 31, 2017
7,682
8,318
60
Australia
✟284,806.00
Country
Australia
Gender
Male
Faith
Atheist
Marital Status
Married
@charsan

The modulus directive means you have to split the number using math rather than string processing.

It's really easy with a string
x = '123'
mylist = [x[0:1],x[1:2],x[2:3]]


However with a number you will need to do something like:
x = int('123')
firstDigit = x/100
x = x % 100 # note % is the python modulo command, asking for the remainder of the division. In this case it will return 23, because 123/100 has 23 as the remainder
secondDigit = x/10
thirdDigit = x % 10
numList = [firstDigit,secondDigit,thirdDigit]
 
Upvote 0

Tanj

Redefined comfortable middle class
Mar 31, 2017
7,682
8,318
60
Australia
✟284,806.00
Country
Australia
Gender
Male
Faith
Atheist
Marital Status
Married
@charsan

Finally, regards classes and object-oriented (OO) programming, these are are a truly excellent technique for ensuring scalable, reusable, readable code. The way to think about it is in terms of objects rather than functions.

Consider a library. From a functional programming perspective you might do something like

def checkOutABoot(bookId)

The problem is what if I want to know something about the book apart from it's ID? I'd then need
def getTheTitleSomehow(bookId)

On the other hand, if I build a Book object, I can then decorate that object with all the information the book knows about itself
book.authors
book.title
book.isdnNumber
etc

So I can handily pass the book object around my code and know all of the stuff the book knows about itself is easily found and in one place.

In OO, functions on classes are called methods, and variables on classes are called properties.
 
Upvote 0

charsan

Charismatic Episcopal Church
Jul 12, 2019
2,297
2,115
54
South California
✟62,421.00
Country
United States
Gender
Male
Faith
Christian
Marital Status
Married
Politics
US-Republican
Hi @Tanj sorry to bother you again. This is what I have worked on so far in two files:

class Lab1:
def __init__(self, x, myList):
self.rawData = int(x)
self.Data = myList() # putting data into a list
def sumNums(self):
sum(x)
return sumNums

In the other file

from Lab1 import Lab1

x = int(input("Please enter an 3 digit number: "))
myObj = Lab1(x)
print(myObj.sumNums)

Now in the class file it doesn't like x in the sum(x) but maybe I am on the right track
 
Upvote 0

Tanj

Redefined comfortable middle class
Mar 31, 2017
7,682
8,318
60
Australia
✟284,806.00
Country
Australia
Gender
Male
Faith
Atheist
Marital Status
Married
Hi @Tanj sorry to bother you again. This is what I have worked on so far in two files:

class Lab1:
def __init__(self, x, myList):
self.rawData = int(x)
self.Data = myList() # putting data into a list
def sumNums(self):
sum(x)
return sumNums

In the other file

from Lab1 import Lab1

x = int(input("Please enter an 3 digit number: "))
myObj = Lab1(x)
print(myObj.sumNums)

Now in the class file it doesn't like x in the sum(x) but maybe I am on the right track

1)
You are using myList in correctly. In the init you are passing it in as a parameter but then in the code calling it as a function.

2) The reason it doesn't like the x in sumNums is because it has no idea what you are talking about. x doesn't exist in the scope of that function. To access properties they need to be set and get with self.

3) Any variables evaporate when the function finishes. In my example below, when init finishes, d1, d2, d3, and temp cease to exist, even though the values that were represented by d1,d2,d3 have been persisted in self.myList.

class Lab1:
...def __init__(self,data):
......self.rawData = int(data)
......d1 = self.rawData/100
......temp = self.rawData % 100
......d2 = temp/10
......d3 = temp % 10
......self.myList = [d1,d2,d3]

...def sumNums(self):
......result = sum(self.myList)
......return result


In the other file

from Lab1 import Lab1
x = input("Please enter an 3 digit number: ")
myObj = Lab1(x)
print(myObj.sumNums())
 
Upvote 0