Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Study Questions
#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


Messages In This Thread
Study Questions - by captainflint - Jan-31-2018, 03:09 PM
RE: Study Questions - by j.crater - Jan-31-2018, 04:09 PM
RE: Study Questions - by sparkz_alot - Jan-31-2018, 09:24 PM
RE: Study Questions - by captainflint - Feb-01-2018, 11:16 AM
RE: Study Questions - by Mekire - Feb-01-2018, 12:45 PM
RE: Study Questions - by captainflint - Feb-01-2018, 03:11 PM
RE: Study Questions - by gruntfutuk - Feb-01-2018, 06:01 PM
RE: Study Questions - by captainflint - Feb-02-2018, 12:27 PM
RE: Study Questions - by DeaD_EyE - Feb-02-2018, 12:55 PM

Forum Jump:

User Panel Messages

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