Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Study Questions
#1
Define a function unique_elem takes two strings str_1 and str_2 as inputs and prints
only the elements that occur in str_1 and not in str_2. Note: You are NOT allowed to
use the in operator.
>> unique_elem(“hello”, “hola”)
e
>> unique_elem(“alyo”, “efendim”)
a
l
y
o

3. Define a function string_reverse that takes a string as input and returns a reversed
version of it.
>> print string_reverse(“sehir”)
rihes

4. Define a function repeat_letters that takes a string and a number n as its two inputs and
prints the string with every letter repeated n times.
>>print repeat_letters(“bla”, 2)
“bbllaa”

pls help me
Reply
#2
What have you tried? Post your code in Python code tags and possible errors (full traceback message) in error tags.
Reply
#3
You are on the right track. The thing is, your function is returning a value, which can be used outside of the function but you need to have a variable to hold that value so that you can utilize it, say in a 'print()' function.

For example:
def sums(num1, num2):
    total = num1 + num2

    return total

answer = sums(5, 5)
print("Answer = ", answer)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
thakns but question is not related to your answer. :D

that question is if ı have two string in func.
if they have same letters print same letters only

for instance:
str1="halo"
str2="hello"

there are same letters which are "h" and "l"
Reply
#5
You need to show some effort. We aren't here to do your work for you. We are here to assist.
Reply
#6
believe me ı have did many things but ı can't reach result. Let me Show you my trials..

def unique_elem(str1,str2): 
#     for i in str1:
#         if i not in str2:
#             
# unique_elem("hello","hola")
Reply
#7
(Feb-01-2018, 03:11 PM)captainflint Wrote: believe me ı have did many things but ı can't reach result. Let me Show you my trials..

def unique_elem(str1,str2): 
#     for i in str1:
#         if i not in str2:
#             
# unique_elem("hello","hola")

Thought that you were not allowed to use the in operator, yet the code you tried (commented out) does have it.

What might be useful for you to know is that the while & for loops have an else clause that is executed if, and only if, the loop completes normally. You can leave a loop early (and not execute the else clause) using the break command.

You already showed a loop that steps through each character of str1. If you have a loop inside that that steps through each character of str2 and breaks if it finds a match you can ignore that character. If you get to the else clause of the inner loop though, you have no match.

Here's a simple programme that uses an else clause.

while True:
    num = input('Enter a number: (return to finish) ')
    if not num: break

    num = int(num)
    for devisor in range(2, 9):
        if num % devisor == 0:
            print(f'Sorry, divisable by {devisor}')
            break
    else:
        print('You have a winner')
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
thank you my friend.
I am in beginner side pytohn because ı've started new.

Thanks a lot..
Reply
#9
If the order doesn't matter, you can use sets.
They come from set theory and do what you want.
Remind: Sets are not ordered and sets do only have unique elements.

set('holla') - set('hello') # the solution
set('holla') | set('hello')
set('holla') & set('hello')
set('holla') ^ set('hello')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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