Python Forum
function help (totaly beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function help (totaly beginner)
#11
If I understand it correctly ,you dont need to use your def main, you can input word in def stars or before def stars (it will be better)

n=str(input())
for character in n:
Try it with this cycle. You just need to add one line.
Good luck
Reply
#12
(Dec-07-2017, 01:10 PM)wavic Wrote: 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.


def all_stars(s):
    # create a variable which will contain the final result and put a '*' in it.
    result = "*"

    # the for loop. A pseudo code. I wont write it for you.
    for each character in s:
        add the character to 'result'
        add a '*' right behind the character

    return the result.
You have to consider that the strings are immutable type of data so adding something to a string is meaning to assign a new value to it:

my_string = 'hello'
my_string = my_string + ', world'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
hi you can use "String Concatenation" to solve your problem
Reply
#14
(Dec-11-2017, 09:54 PM)wavic Wrote:
(Dec-07-2017, 01:10 PM)wavic Wrote: 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.


def all_stars(s):
    # create a variable which will contain the final result and put a '*' in it.
    result = "*"

    # the for loop. A pseudo code. I wont write it for you.
    for each character in s:
        add the character to 'result'
        add a '*' right behind the character

    return the result.
You have to consider that the strings are immutable type of data so adding something to a string is meaning to assign a new value to it:

my_string = 'hello'
my_string = my_string + ', world'

Thanks! I gott it. The first function works and prints the stars.
Now i must add a new funktion that "encrypts" the input word, somehing like this: ABCD gets BCDE...moving the letters of the word by one.
I thougt i make an array with the alfabet first...but i'm stuck of course! This is the code so far:

alfabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
def stars(s):
    for i in range(len(s)):
        print("*",s[i],sep='',end='')
    print("*")


def krypt():
    for i in(alfabet):
        print()


text = input("write a word ")
stars(text)
krypt()
print()
Reply
#15
The for loop in stars function could look like this:
I have changed the names of the variables so now if you look at it, you know what is going on. Single letter var names don't speak too much for themselves.
You don't need the index to get each value of an iterable.

def stars(word):
    for char in word : # on every iteration char holds the next letter of the word
        print("*", char, sep='', end='')
    print("*")
The "encryption"...
Do you care if each letter is lower or uppercase? If you do, then you need all letters. Then you need two lists.

The programming is not as complicated as the most people think it is. It could and it is. Very. But when it comes to what you are studying right now, all it's needed is to imagine, how would you do it by hand. And implement it in a code.

So you take a letter of the word and you get the index of that letter in the list and move it to the right - add 1 to the index. Check if the letter is the last one and take the first one if that is the case. Otherwise, you get an error: List index out of range.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#16
(Dec-12-2017, 08:48 PM)wavic Wrote: So you take a letter of the word and you get the index of that letter in the list and move it to the right - add 1 to the index. Check if the letter is the last one and take the first one if that is the case. Otherwise, you get an error: List index out of range.

Thank you wavic.
I am on an online course that i have regret. The help i gett is almost zero and the book i got is not completely accurate to the assigments.
I feel that if i see this assigment solved i will get unstuck. I will try to continue the course in the classroom next year.
I hope that you can help me a little bit more but i understand if you wont.
cheers!
Reply
#17
If you are using Python 3.x then you can try the end keyword(*For printing the Stars*)
print ("Enter the Data: ")
Data=input()
print('*',end='')
for i in Data:
    print(i,end='*')
Regards,
Rizvi
Reply


Forum Jump:

User Panel Messages

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