Question

Python3 command line arguments; there are unknown number of arguments, 'only number // number and the length of the argument is three' is a valid argument,for example,2//5,3//5 are valid arguments,a//2,b//4,7//0,b//k are not valid arguments, calculate each valid argument and the number of valid arguments, and then print

hint: split is very useful

restrictions: [1:], for loop, slicing, lambda function, 'in' keyword,isdigit(), isalpha() cannot be used. Exception handling must be used, it is recommended to use while loop to solve this problem.

Cuser@sahara ]$ python Q3.py 2//0 a//b 3//a b//2 8//3 9//4 5/2 8//3 2 9//4 2 There are 2 valid arguments Cuser@sahara ~] pyth

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer...

CODE:

import sys
def find(s):
i=0
#print(s)
while(i<len(s)):
if s[i]=='/':
return i
i+=1
def join(l):
i=0
s=""
while(i<len(l)):
s+=l[i]+" "
i+=1
return s
length=len(sys.argv)
sys.argv=join(sys.argv)
a=sys.argv.split(" ")
#print(a)
index=1
l=[]
while(index<length):
l.append(a[index]) #append each argument to list
index+=1

j=0
count=0 #to take 3 number// number
valid=0 #for valid count
while(j<len(l)): #iterate through list of numbers
i=0
#print(l[j])
k=find(l[j]) #find the // operator in argument
#print(k)
if(count==3): #if count 3 break
break
s1="" #for storing first number
while(i<k):
s1+=l[j][i] #concatenate to string
i+=1
i+=2
#print(i)
s2="" #for denominator/second number
while(i<len(l[j])):
s2+=l[j][i] #concatenate to second string
i+=1
#print(s1,s2)
j+=1 #for next argument
try:
a=int(s1) #convert first number to integer
b=int(s2) #convert second number to integer
#print(a,b)
count+=1 #count the number//number arguments
#if possible then print and count valid arguments
print(a,"//",b," = ",a//b)
valid+=1
  
except:
print(end="") #cathing the raised exception and do nothing
print("There are ",valid," valid arguments") #print valid arguments
  
  
  


  

CODE Snapshot:

- 0 X 3 File Users\sai Desktop 03.py - Sublime Text (UNREGISTERED) Edit Selection Find View Goto Tools Project Preferences Hes1- $2 k-find(1[j]) #find the // operator in argument #print(k) if(count==3): #if count 3 break break #for storing first nu

OUTPUT:

C:\WINDOWS\system32\cmd.exe - O X C:\Users\sai\Desktop>python 03.py_27/0 a/b 3//a b//28/73 9//4 5/2 8 // 3 = 2 9 // 4 = 2 The

If you have any doubts please COMMENT...

If you understand the answer please give THUMBS UP....

Add a comment
Know the answer?
Add Answer to:
Python3 command line arguments; there are unknown number of arguments, 'only number // number and the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT