Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
Created August 19, 2020 04:23
Show Gist options
  • Save guestPK1986/5411dceacb950bbcf2272d26dd236e8d to your computer and use it in GitHub Desktop.
Save guestPK1986/5411dceacb950bbcf2272d26dd236e8d to your computer and use it in GitHub Desktop.
course_1_assessment_8
#Create one conditional so that if “Friendly” is in w, then “Friendly is here!” should be assigned to the variable wrd.
#If it’s not, check if “Friend” is in w. If so, the string “Friend is here!” should be assigned to the variable wrd,
#otherwise “No variation of friend is in here.” should be assigned to the variable wrd
w = "Friendship is a wonderful human experience!"
for x in w:
if "Friendly" in w:
wrd = str("Friendly is here!")
elif "Friend" in w:
wrd = str("Friend is here!")
else:
wrd = str("No variation of friend is in here.")
print(wrd)
#We have written conditionals for you to use.
#Create the variable x and assign it some integer so that at the end of the code,
#output will be assigned the string "Consistently working".
x = 8
if x >= 10:
output = "working"
else:
output = "Still working"
if x > 12:
output = "Always working"
elif x < 7:
output = "Forever working"
else:
output = "Consistently working"
#Write code so that if "STATS 250" is in the list schedule,
#then the string "You could be in Information Science!" is assigned to the variable resp.
#Otherwise, the string "That's too bad." should be assigned to the variable resp.
schedule = ["SI 106", "STATS 250", "SI 110", "ENGLISH 124/125"]
if "STATS 250" in schedule:
resp = "You could be in Information Science!"
else:
resp = "That's too bad."
print(resp)
#For each string in wrd_lst, find the number of characters in the string.
#If the number of characters is less than 6, add 1 to accum so that in the end,
#accum will contain an integer representing the total number of words in the list that have fewer than 6 characters.
wrd_lst = ["Hello", "activecode", "Java", "C#", "Python", "HTML and CSS", "Javascript", "Swift", "php"]
accum = 0
for x in wrd_lst:
if len(x)<6:
accum = accum +1
print(accum)
@hadrocodium
Copy link

hadrocodium commented Oct 24, 2021

course_1_assessment_8.py

First, you do not need for loop. And it does not do what you expect. The loop variable x is a character in the w string. In other words, x is "F' and it is not 'Friendship', and so on. The reason why your code works is what is in the loop body. Even there you do not need str function because text under quotes is a string.

w = "Friendship is a wonderful human experience!"

if 'Friendly' in w:
    wrd = 'Friendly is here!'
elif 'Friend' in w:
    wrd = 'Friend is here!'
else:
    wrd = 'No variation of friend is in here.'

print(wrd)

If you want to do it using for loop, you will need to split string w into the list of strings(words) where the separator is space. Because the space is the default separator in string method split you can use split() but also you can use split(' '). Both of these represent space as a separator. Once the condition is True (like if 'Friend' in word is True), it will execute its block of code, in this case wrd = 'Friend is here!'. You need to add a statement break in order to exit the for loop entirely because for loop will continue to parse through list words for each element of the list (word is an element of the list words). So if you do not put break statement, for loop will always get its result based on the last word/string in this case 'experience!'.

w = "Friendship is a wonderful human experience!"

words = w. split()

for word in words:
    #print('***', word)
    if 'Friendly' in word:
        wrd = 'Friendly is here!'
        break
    elif 'Friend' in word:
        wrd = 'Friend is here!'
        break
    else:
        wrd = 'No variation of friend is in here.'

print(wrd)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment