Python Forum
Homework about string conversions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework about string conversions
#10
Try entering the following into the interactive shell for python 3:

'What the f*** went wrong?'.split()
['Read', 'The', 'F******', 'Manual'][0][0].isupper()

(Dec-03-2017, 12:29 PM)stiletto009 Wrote: okay so im typing down what i know for now.

def get_acronym(s):
    a=""
    for i in s:
        if letter.isupper() True:
            
   
our lecturer didnt explain commands and stuff.we just copy pasted in class so now im stuck here.
Ok, let's explain what you have.

def is the start of a definition of a block of code (indented by 4 spaces under the row starting def) that constitutes a function. Much like sin in mathematics is a function that when used in the form sin(x) will return the sin value of x in place of wherever it was referenced.

A function in programming can be used to return not just simple mathematical values, but strings of characters and even complex data structures.

So, for the square of a number, you could write the function:
(This is a very clumsy example.)

def mysquare(x):
    y = x * x
    return y
    
mynum = 10
squareofmynum = mysquare(mynum)
print(squareofmynum)
A function needs to return a value when it finishes executing. It can have a return statement anywhere, and execution of a function ends when it encounters a return statement. Programme flow continues from where it was called.

A function is not much use unless it is called. In the example, I could have called it from the print statement: print("Square of 64 is:", mysquare(64))

In an if statement, you evaluate an expression (a calculation, comparison of strings, some other test) to see if the result is either True or False. If the result is True, all the statements under the if line and indented 4 spaces are executed.

Here's an example:

num = -23
if num > 0 :
    print('Hello. I have a positive number.')
    if num % 2 == 0:
        print("It is an even number.")
    else:
        print("It is an odd number")
else:
    if num == 0:
        print('I have nothing to say.')
    else:
        print('I am feeling negative.')
So, you know how to write a function. You know how to use an if statement.

You've already shown some understanding of lists and using loops.

Some clues have been given on how to do what you need to do.

I think you probably have what you need to make a very good attempt.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
Homework about string conversions - by stiletto009 - Dec-03-2017, 11:22 AM
RE: Homework about string conversions - by Larz60+ - Dec-03-2017, 11:54 AM
RE: Homework about string conversions - by buran - Dec-03-2017, 12:15 PM
RE: Homework about string conversions - by buran - Dec-03-2017, 12:19 PM
RE: Homework about string conversions - by gruntfutuk - Dec-06-2017, 10:40 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020