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
C programming 3. Write a do-while loop that displays: 0,10,20,30,40 4.Write nested for loops that displays...
C programming 3. Write a do-while loop that displays: 0,10,20,30,40 4.Write nested for loops that displays three rows of asterisks: ***** ***** ***** 5. Write a complete program that reads a set of integers using scant() until a sentinel value of -99 is read. Once that value is read, display the sum of the value read, exclusive of the -99. 6.Write a procedure(function, method named times10() that accepts an integer argument and displays the result of multiplying the argument by...
Using Java: While and for loop to generate numbers in the Fibonacci sequence Tasks: 1. Using...
Using Java: While and for loop to generate numbers in the Fibonacci sequence Tasks: 1. Using your understanding of Fibonacci numbers chalk out how you would use a while loop to generate (and display) numbers in the Fibonacci sequence. The program that you are attempting to write must take a number, n, from the user that tells you how many Fibonacci numbers to display. Write the while loop portion of how you think this would work below. (You do not...
IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible...
IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible for typing (the driver) and the other reviewing each line of code and making suggestions (the navigator) you must switch roles after every problem. 1). List Construction Write a Python program that prompts the user to enter a sequence of lowercase words and stores in a list only those words whose first letter occurs again elsewhere in the word (e.g., "baboon"). Continue entering words...
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
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)...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
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...