Python Forum
function help (totaly beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function help (totaly beginner)
#7
At least you've made a start and put some structure in place.

def stars(s):
    letters = len(s)
    n = letters-1
    for i in range(letters):
You don't need to check the length of a string in Python for this because you can do a loop using for character in s: and each time the loop goes round, the variable character (in my example) will hold a copy of each successive character in the source string s. (Note that generally we avoid single letter variable names as they are not very informative.)

Before the loop starts, create a variable pointing to an empty string, e.g. starword = "", and then each time through the loop you can add the character and a star. When you have finished, you can return the final new string with the return statement: return starword.

def main():
    word = input("write a word ")
    print(word)
Ok, so this gets some input and prints it out. You need to add a call to your function before the print out to give a new version of word in it with the stars that you want added. You could do word = stars(word).
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
function help (totaly beginner) - by thanikos - Dec-07-2017, 12:32 PM
RE: function help (totaly beginner) - by thanikos - Dec-07-2017, 12:44 PM
RE: function help (totaly beginner) - by wavic - Dec-07-2017, 12:50 PM
RE: function help (totaly beginner) - by thanikos - Dec-07-2017, 12:56 PM
RE: function help (totaly beginner) - by wavic - Dec-07-2017, 01:10 PM
RE: function help (totaly beginner) - by gruntfutuk - Dec-07-2017, 03:07 PM
RE: function help (totaly beginner) - by thanikos - Dec-10-2017, 11:25 PM
RE: function help (totaly beginner) - by wavic - Dec-11-2017, 02:02 AM
RE: function help (totaly beginner) - by thanikos - Dec-11-2017, 08:55 AM
RE: function help (totaly beginner) - by Item97 - Dec-11-2017, 08:57 AM
RE: function help (totaly beginner) - by wavic - Dec-11-2017, 09:54 PM
RE: function help (totaly beginner) - by thanikos - Dec-12-2017, 07:12 PM
RE: function help (totaly beginner) - by wavic - Dec-12-2017, 08:48 PM
RE: function help (totaly beginner) - by thanikos - Dec-13-2017, 08:32 PM

Forum Jump:

User Panel Messages

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