Unable To Load Module

Hello.

Pretty sure nobody remember me. I discovered Python a few months ago. I came here, and a couple of people helped me get started and I appreciate it. Then a bunch of problems, both personal and digital, hit me all at once. I’m back now. New computer. (Unfortunately, I still have the same bad attitude.)

Just started practicing with Python again today, I was able to use the suppled modules before, but I’m unable to now. Maybe I need to type the path. Or I’m doing something else wrong.

Shouldn’t this code work:

import string
BN = ‘BNnMuy’
BY = lower (BN)
print (BY)

I get message ‘lower’ not defined.

Any idea??

Thank you,

lower is not a (global) function. You can get a list of built-in functions here: Built-in Functions — Python 3.9.4 documentation

What you seem to want is the lower method of str objects:

bn = 'BNnMuy'
by = bn.lower()
print(by)

Another thing: importing a module like import string doesn’t give you access to its functions in the way you think: you have to access the functions like this: string.capwords(bn)


PS: to post code to this website, wrap it in ``` (` is called a back-tick, acute or left-quote). For example:

```python
import string
BN = 'BNnMuy'
BY = lower(BN)
print(BY)
```

“lower” is a string method. You have a string in BN. So to get it in
lower case you call its lower() method:

BY = BN.lower()

Cheers,
Cameron Simpson cs@cskk.id.au

Laurie O and Cameron,

Thank you! I was thinking I had to type the entire path to the module.

Not sure what proper protocol is for asking another question. Should I append it to this thread I already started?? Or should I start a new thread??

Anyway, I can’t get ‘\r’ to work like I think it’s supposed to. Here is code from another web site:

text = "Here's a piece of text I want to overwrite" 
repl = "BALEETED!" # What we want to write
print(text, end="\r") # Write the text and return
time.sleep(1)
print(f'\r{repl: <{len(text)}}') 

This SHOULD print "Here’s a piece …’ on the screen, leave it there for a second then replace it with "BALEETED,’

What it does is print the text, leave it there for a second, then appends repl. Looks like: Here’s a piece of text I want to overwriteBALEETED

What am I doing wrong??

The python is probably doing what is expected. But not every terminal will react the same way to the output. Depends on the specific terminal that is interpreting the output. On a Mac “terminal” or “iTerm2” it works fine.

Not sure what proper protocol is for asking another question. Should I
append it to this thread I already started?? Or should I start a new
thread??

It’s a new question. Start a new thread. But not this time; do that next
time.

Anyway, I can’t get ‘\r’ to work like I think it’s supposed to. Here is code from another web site:

text = "Here's a piece of text I want to overwrite"
repl = "BALEETED!" # What we want to write
print(text, end="\r") # Write the text and return
time.sleep(1)
print(f'\r{repl: <{len(text)}}')

This SHOULD print "Here’s a piece …’ on the screen, leave it there for a second then replace it with "BALEETED,’

What it does is print the text, leave it there for a second, then appends repl. Looks like: Here’s a piece of text I want to overwriteBALEETED

What am I doing wrong??

I guess it may depend on where you’re running it. In a real terminal it
looks like it should work (but include “flush=True” in the first print()
to make sure it goes out promptly).

Are you running this in some IDE GUI? Maybe it does not honour \r like a
terminal does.

But to my eyes, i would expect what you’ve done to work, and I speak as
someone with code critically dependent on doing exactly what you’re
doing.

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you for the replies, but I’m just not getting it. First of all, what do you mean by “console??” Do you mean what monitor am I using?? Ordinary Dell monitor.

Do you mean, where does the program output go?? Well, it goes to Idle. The Idle that came with Python. One Window in which to write code and another Window where the program output and error messages go. I assume that “Shell” will do whatever the code says.

Or does console mean something else altogether??

And I can’t figure out how to use “flush.”

Here’s what I’ve tried:

import time

print ('HOO', sys.stdout.flush (), '\r')
#sys.stdout.flush () <- Commented this line out.
time.sleep (1)
print ('New')

Result is:
Hoo None
New

Guys, I appreciate your help and I am embarrassed to be so stupid.

Thank you for the replies, but I’m just not getting it. First of all,
what do you mean by “console??”

Can’t see “console” in the discussion. But in general the “console” is
the keyboard/monitor directly attached to the computer you’re using. BUT
it usually means when tht computer is in text mode, not running a GUI
desktop.

Do you mean what monitor am I using?? Ordinary Dell monitor.

Thanks. But should be irrelevant.

Do you mean, where does the program output go?? Well, it goes to Idle. The Idle that came with Python. One Window in which to write code and another Window where the program output and error messages go. I assume that “Shell” will do whatever the code says.

Ok. IIRC, IDLE doesn’t entirely act like a normal terminal emulator.

Or does console mean something else altogether??

And I can’t figure out how to use “flush.”

Here’s what I’ve tried:

import time

print ('HOO', sys.stdout.flush (), '\r')
#sys.stdout.flush () <- Commented this line out.

Either:

# print string, end the "line" with a \r, and get print to flush the 
# output immediately
print ('HOO', end='\r', flush=True)

or:

# print string, end the "line" with a \r
print ('HOO', end='\r')
# flush the output now instead of asking print to do it
sys.stdout.flush()

The latter form is seen when you do a bunch of prints and a single flsh
at the end, when it is necessary to ensure the output has gone to the
terminal.

Guys, I appreciate your help and I am embarrassed to be so stupid.

You’re not stupid, you’re just new to this.

Cheers,
Cameron Simpson cs@cskk.id.au

OK, thanks Cameron.