0% found this document useful (0 votes)
10 views2 pages

Python Tour2 TypeC With Aman

The document contains two Python programming exercises. The first exercise involves validating a phone number format with specific conditions, while the second exercise requires analyzing user-inputted sentences for word count, character count, and alphanumeric percentage. Both exercises include sample code to demonstrate the required functionality.

Uploaded by

aman11211369y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Python Tour2 TypeC With Aman

The document contains two Python programming exercises. The first exercise involves validating a phone number format with specific conditions, while the second exercise requires analyzing user-inputted sentences for word count, character count, and alphanumeric percentage. Both exercises include sample code to demonstrate the required functionality.

Uploaded by

aman11211369y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Tour 2 - Type C Programming Practice

1. Write a program that prompts for a phone number of 10 digits and two dashes, with

dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal

input. Display if the phone number entered is in a valid format or not and display if the phone

number is valid or not (i.e., contains just the digits and dash at specific places).

# Prompt the user for input


phNo = input("Enter the phone number: ")

# Check if the length is 12 and dashes are at correct positions


if len(phNo) == 12 and phNo[3] == "-" and phNo[7] == "-" and phNo[:3].isdigit() and
phNo[4:7].isdigit() and phNo[8:].isdigit():
print("Valid Phone Number")
else:
print("Invalid Phone Number")

2. Write a program that should prompt the user to type some sentence(s) followed by 'enter'.

It should then print the original sentence(s) and the following statistics relating to the

sentence(s): - Number of words, - Number of characters (including white-space and

punctuation), - Percentage of characters that are alphanumeric.

# Input from user


str = input("Enter a few sentences: ")
length = len(str)
spaceCount = 0
alnumCount = 0

# Count spaces and alphanumeric characters


for ch in str:
if ch.isspace():
spaceCount += 1
elif ch.isalnum():
alnumCount += 1

Made with ChatGPT with Aman


Python Tour 2 - Type C Programming Practice

# Calculate alphanumeric percentage


alnumPercent = alnumCount / length * 100

# Print results
print("Original Sentences:")
print(str)
print("Number of words =", (spaceCount + 1))
print("Number of characters =", length)
print("Alphanumeric Percentage =", alnumPercent)

Made with ChatGPT with Aman

You might also like