USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the second word. The second word may begin with either an upper or lower case instance of firstLetter.The function twoWords should return the two words in a list.Use a while True loop and a break statement in the implementation of twoWords. 2-Write a function named twoWordsV2 that has the same specification as Problem 1, but implement it using while and not using break. (Hint: provide a different boolean condition for while.) Since only the implementation has changed, and not the specification, for a given input the output should be identical to the output of problem 1 3-Write a function geometric ()that takes a list of integers as inputs and returns true if the integers in the list form a geometric sequence.(hint a sequence is geomtric if the ratio of a0/a1, a2/a1,a3/a2 are all equal) 4-implemet a function mystery() that takes as input a positive integer n and answer this question, how many times can n be halved(using integer division) before reaching 1? 5- Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
from __future__ import division
def twoWords(length, first_char):
words = []
while True:
word = raw_input("Enter a word of " + str(length) + " characters :
")
if len(word) != length:
print "Please enter only " + str(length) + " characters"
continue
else:
words.append(word)
break
while True:
word = raw_input("Please enter a word starting with character '" +
first_char + "' : ")
if len(word) == 0 or (word[0].lower() != first_char.lower()):
print "Please only enter word starting with '" + first_char +
"'"
continue
else:
words.append(word)
break
return words
def twoWordsV2(length, first_char):
words = []
while len(words) != 2:
if len(words) == 0:
word = raw_input("Enter a word of " + str(length) + " characters :
")
if len(word) != length:
print "Please enter only " + str(length) + " characters"
continue
else:
words.append(word)
if len(words) == 1:
word = raw_input("Please enter a word starting with character '" +
first_char + "' : ")
if len(word) == 0 or (word[0].lower() != first_char.lower()):
print "Please only enter word starting with '" + first_char +
"'"
continue
else:
words.append(word)
return words
def geometric(sequence):
if len(sequence) <=2:
return True
if sequence[0] == 0:
return False
ratio = sequence[1]/sequence[0]
for i in range(2, len(sequence)):
if (sequence[i] == 0) or (ratio !=
(sequence[i]/sequence[i-1])):
return False
return True
def mystery(n):
count = 0
if n < 2:
return 0
while(n != 1):
n = n // 2
count = count + 1
return count
def enterNewPassword():
while True:
password = raw_input("Enter password with length 8-15 and having
atleast one digit: ")
if len(password) >= 8 and len(password) <= 15 and
any(char.isdigit() for char in password):
break
if len(password) < 8:
print "Password length is too short"
elif len(password) > 15:
print "Password two big"
if not any(char.isdigit() for char in password):
print "Password should contain atleast one digit"
print twoWords(10, 'a')
print twoWordsV2(10, 'a')
print geometric([1,2,4,8,16])
print geometric([1,2,4,8,10])
print mystery(100)
print mystery(2)
print mystery(1)
print mystery(50)
enterNewPassword()
# pastebin link for code: http://pastebin.com/9GWWAe11
Get Answers For Free
Most questions answered within 1 hours.