Python Forum
function help (totaly beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function help (totaly beginner)
#1
Hello i have this assignment and i don't know how to continue.
User will write a word , example, table, and the program will print *t*a*b*l*e*

I appreciate your help

Here is where i am with my code:

def stars(s):
    letters = len(s)
    n = letters-1
    for i in range(letters):




def main():
    word = input("write a word ")
    print(word)


if __name__=='__main__':
    main()
Reply
#2
Can you explain the task more clearly, also can you give an example of an input and its output?
Reply
#3
The program asks the user to input any word, and the program will print the same word but with stars * between all the letters of the word. example if user inputs a word like "computer" the program will print out *c*o*m*p*u*t*e*r*
Reply
#4
It's not clear how you should do the task. Is it allowed to use join() method or only to iterate over the word and construct the final result manually? Can you use format() method?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
We haven't learn the join method yet, I think it is only to iterate over the word and construct the final result manually. format method is propably ok too
Reply
#6
Why do you need 'n'?

How to iterate over a string?
for letter in word: # here the word is whatever word you like.
    print(letter) # letter is holding the next letter of the word on each iteration. 
create a variable which will contain the final result and put a '*' in it during the initialization. On each iteration add a letter to it followed by a '*'. and you get the final result as requested. All this inside the function.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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
#8
(Dec-07-2017, 03:07 PM)gruntfutuk Wrote: 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).

Hello
Something like this? How do i add the stars?

def stars(s):
    starword = ""
    for character in s:
        return starword


def main():
    word = input("write a word ")
    word = stars(word)
    print(word)
Reply
#9
Shy I told you how. Just fallow the steps.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Dec-11-2017, 02:02 AM)wavic Wrote: Shy I told you how. Just fallow the steps.

I still can't fix it, i'm a total beginner,but Thank you anyway for trying to help.
Reply


Forum Jump:

User Panel Messages

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