Skip to content

Forum in maintenance, we will back soon 🙂

Notifications
Clear all

Python Script

43 Posts
4 Users
21 Reactions
587 Views
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

Hi I am going through the course and am on the loop module. I added the following code, it prints "Even number detected" but it doesnt print the odd number, what is wrong please?

python loop

 

 
Posted : 03/21/2024 10:16 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-rayazsiddiqi you need to indent the "else:" statement so that it is in the same column position as the "if" statement.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/22/2024 12:33 am
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

@ssadvisor Many thanks Ernie, really appreciate it, I'll give it a try!

 
Posted : 03/22/2024 9:41 am
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

@ssadvisor Thanks Ernie, it's working now!!

 
Posted : 03/24/2024 2:28 pm
SSAdvisor reacted
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

Can someone explain what this code does: sum_numbers+=number

What does the "+" sign do?

 

Thanks

 

 
Posted : 03/25/2024 5:34 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-rayazsiddiqi It's a shorter version of

sum_numbers = sum_numbers + number

Those who program like to program in short cuts so we don't have to type as much. You will also see:

a -= b
a *= b
a /= b

You can read it as take the value of the left variable, do the indicated arithmetic with the value of the right variable and then store the result into the left variable.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/25/2024 7:23 pm
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

@ssadvisor thanks very much, makes sense now!

 
Posted : 03/25/2024 9:30 pm
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

Hi, I am running the module script and it sin't working:

This is the module code:

def print_list_elements(my_list):
for element in my_list:
print(f"{element}")

And here is the script calling the module:

import my_module
fruits = ["apple", "banana", "cherry"]
my_module.print_list_elements(fruits)

 

This is the error I am getting:

AttributeError: module 'my_module' has no attribute 'print_list_elements'

Can someone please help?

Many Thanks

 

 
Posted : 03/28/2024 8:46 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-rayazsiddiqi You've got to share this code with the code icon (<>) above to preserve the formatting. Python is indentation sensitive.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/29/2024 4:03 am
(@husein)
Posts: 464
Member Moderator
 

@google-rayazsiddiqi, please do what @ssadvisor mentioned to see if indentation in the code caused the problem. 

But, anyway, are both files in the same folder? This error used to pop up when I had the module file outside the main script folder.

 
Posted : 03/29/2024 9:24 am
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

@ssadvisor Hi I couldn't find the <> code icon, so please see screenshots below, thanks for your help

module code
main code
 
Posted : 03/29/2024 2:59 pm
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 

@husein hi, the module code and the script code are in the same folder (althouhg this is not the same as the Pythin folder, is this the problem)?

 
Posted : 03/29/2024 3:00 pm
(@google-rayazsiddiqi)
Posts: 95
Estimable Member
Topic starter
 
folders
 
Posted : 03/29/2024 3:00 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@google-rayazsiddiqi I see from this picture that you haven't saved the changes to the my_modules.py file. In VS Code when in that module enter CTRL-S to save the code.

You should see the code icon <> when you comment in the middle of the icons.

image

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/30/2024 2:24 am
(@husein)
Posts: 464
Member Moderator
 

@google-rayazsiddiqi They should show that they're in the same folder in the VS code, too. 

I see it's very messy in VS code; try putting every project alone in a distant folder. It will facilitate navigation and provide a clear structure of your codes and folders (this has nothing to do with errors; it's to make things clearer for you)

 
Posted : 03/30/2024 8:50 am
Page 1 / 3
Share: