Question

USING PYTHON do all the he problems using while loop , continue and break 1-This problem...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes a ransom note. It is not wrriten by hand to avoid having his hand writing being recognized, so the kid napper uses a magazine to create a ransom note. We need to find out, given the ransom note string and magazine string, is it possible to given ransom note. The kidnapper can use individual characters of words. Here is how your program should work...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT