Question

Python Please. a)Let a program store the result of applying the eval function to the first...

Python Please.

a)Let a program store the result of applying the eval function to the first command-line argument. Print out the resulting object and its type. Run the program with different input: an integer, a real number, a list, and a tuple. (On Unix systems you need to surround the tuple expressions in quotes on the command line to avoid error message from the Unix shell.) Try the string "this is a string" as a commandline argument. Why does this string cause problems and what is the remedy?

b) Prompt the user for input to a formula. Consider the simplest program for evaluting the formula y(t) = v0t− 0.5gt2 [V(base 0)t - 0.5gt(square)]. : v0 = 3; g = 9.81; t = 0.6 y = v0*t - 0.5*g*t**2 print y Modify this code so that the program asks the user questions t=? and v0=?, and then gets t and v0 from the user’s input through the keyboard.

c)Modify the program listed in b) such that v0 and t are read from the command line.

d)Extend that program with exception handling such that missing command-line arguments are detected. In the except IndexError block, use the raw_input function to ask the user for missing input data. e)Test if the t value read in the program from Exercise 4.7 lies between 0 and 2v0/g. If not, print a message and abort execution.

e)Instead of printing an error message and aborting the program explicitly, raise a ValueError exception in the if test on legal t values in the program from d) Include the legal interval for t in the exception message.

f)A car driver, driving at velocity v0, suddenly puts on the brake. What braking distance d is needed to stop the car? One can derive, from basic physics, that d= 1/2 Vo2/µg [formula in english : d= 1 by 2 multiplied V(base zero)- squared divided by (/)µg] Make a program for computing d in (b) when the initial car velocity v0 and the friction coefficient µ are given on the command line. Run the program for two cases: v0 = 120 and v0 = 50 km/h, both with µ = 0.3 (µ is dimensionless). (Remember to convert the velocity from km/h to m/s before inserting the value in the formula!)

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

a)

i 2 3 import sys a=sys.argv[1] #getting first command line as input try: print(eval(a)) #print the result of command line arg

Sample output:

$python problem.py 6 6 <type int> rint (type (eval(a))) [anonymus@parrot]-[-/Desktop/HomeworkLib Error $python problem.py -45 -45

Rawcode:

import sys
a=sys.argv[1]       #getting first command line as input
try:
    print(eval(a))       #print the result of command line arguement after evaluation
    print(type(eval(a)))   #printing type of result
except (NameError, SyntaxError):   #execption handling for strings as commandline
    print(a)
    print(type(a))

--------------------------------------------------------------------------

b)

Before modification:

1 g=9.81 2 Vo=3 3 t=0.6 4 y=vo*t-0.5*g*t**2 5 print(y2

output:

File Edit View Search Terminal Help lanonymus@parrot]-1-/Desktop/HomeworkLib. $python3 formula.py 0.034199999999999786 anonymus@par

RawCode:

g=9.81
vo=3
t=0.6
y=vo*t-0.5*g*t**2
print(y)

--------------------------------

After modification:

i | 2 3 4 5 g=9.81 vo=float(input(Vo= ), t=float(input(t= )), y=vo*t-0.5*g*t**2 print(y)

sample output:

PYTICE vo= 4 t= 1 [anonymus@parrot]-[-/Desktop/cheg

Rawcode:

g=9.81
vo=float(input("vo= "))
t=float(input("t= "))
y=vo*t-0.5*g*t**2
print(y)

---------------------------------------

c)

1 2 3 4 5 6 import sys g=9.81 vo=eval(sys.argv[i]) treval(sys.argv[2]). y=vo t-0.5*g*t**2 print(y) #eval for converting strin

sample Output:

[anonymus@parrot]-[-/Desktop/HomeworkLib] $python formula.py 5 3 -29.145 Canonymus@parrot-(-/Desktop/HomeworkLib] Solutions

Rawcode:

import sys
g=9.81
vo=eval(sys.argv[1])   #eval for converting string to float or integer
t=eval(sys.argv[2])
y=vo*t-0.5*g*t**2
print(y)

---------------------------------------------

d)

1 import sys GAWN try: vo=eval (sys.argv[1]) 5 t=eval(sys.argv[2]). 6 except IndexError: t=eval (raw_input(t=)) 8 g=9.81 9

Sample output:

LUNUITYu JUULIUL LIDLJICOP CUCYy - $python formula.py 3 argi t=0.6 0.0342 IndexError: [anonymus@parrot]-[-/Desktop/HomeworkLib]) $

Rawcode:

import sys

try:
   vo=eval(sys.argv[1])
   t=eval(sys.argv[2])
except IndexError:
    t=eval(raw_input("t="))
g=9.81
y=vo*t-0.5*g*t**2
print(y)

-------------------------------------------------------------

e)

import sys try: Vo=eval(sys.argv[1]) t=eval(sys.argv[2]) except IndexError: t=eval (raw input(t=)), g=9.81 if(0<=t<=2*vo*g)

output:

L $python formula.py 3 t=6 ok y VotSgt 2 - 158.58int(y) (anonymus@parrot -[-/Desktop/HomeworkLib. -- $python formula.py 3 t=88 not

Rawcode:

import sys

try:
   vo=eval(sys.argv[1])
   t=eval(sys.argv[2])
except IndexError:
   t=eval(raw_input("t="))
g=9.81
if(0<=t<=2*vo*g):
   print("ok")
else:
   print("not valid")
   exit(0)
y=vo*t-0.5*g*t**2
print(y)

------------------------------------------------------------

f)

1 Imp import sys 2 vo=eval (sys.argv[1]) 3 u=eval (sys.argv[2]) 4 g=9.81 5 Vo=v0*18/5 #convertion from kmph to mps 6 d=(1/2)

output:

lanonymus@parrot-r-/Desktop/HomeworkLib. $python3 formula.py 120 0.3 146.78899082568807 [anonymus@parrot -[-/Desktop/HomeworkLib) $python

Rawcode:

import sys
vo=eval(sys.argv[1])
u=eval(sys.argv[2])
g=9.81
vo=vo*18/5   #convertion from kmph to mps
d=(1/2)*vo*2/(u*g)
print(d)

----------------------------------------------

Hope you understand.If you any doubts comment me.Upvote me.

i 2 3 import sys a=sys.argv[1] #getting first command line as input try: print(eval(a)) #print the result of command line arguement after evaluation print(type (eval(a))) #printing type of result except (NameError, SyntaxError): #execption handling for strings as commandline, print(a) print(type(a)) 6

$python problem.py 6 6 <type 'int'> rint (type (eval(a))) [anonymus@parrot]-[-/Desktop/HomeworkLib Error $python problem.py -45 -45 mint type a.) <type 'int'> (anonymus@parrot]-[-/Desktop/HomeworkLib) $python problem.py -45.4 -45.4 <type 'float'> (anonymus@parrot -[-/Desktop/HomeworkLib. - $python problem.py "[1,2,3,4]" [1, 2, 3, 4] <type 'list'> (anonymus@parrot]-[-/Desktop/HomeworkLib) $python problem.py "(3,4,3)" (3, 4, 3) <type 'tuple'> (anonymus@parrot]-[-/Desktop/HomeworkLib. L $python problem.py "this is a string" this is a string <type 'str'> [anonymus@parrot]-[-/Desktop/HomeworkLib]

1 g=9.81 2 Vo=3 3 t=0.6 4 y=vo*t-0.5*g*t**2 5 print(y2

File Edit View Search Terminal Help lanonymus@parrot]-1-/Desktop/HomeworkLib. $python3 formula.py 0.034199999999999786 anonymus@parrot)-(-/Desktop/HomeworkLib Solutions $python formula.py 0.0342 Canonymus@parrot 1-[-/Desktop/HomeworkLib mit the

i | 2 3 4 5 g=9.81 vo=float(input("Vo= "), t=float(input("t= ")), y=vo*t-0.5*g*t**2 print(y)

PYTICE vo= 4 t= 1 -0.905 [anonymus@parrot]-[-/Desktop/cheg

1 2 3 4 5 6 import sys g=9.81 vo=eval(sys.argv[i]) treval(sys.argv[2]). y=vo t-0.5*g*t**2 print(y) #eval for converting string to float or integer

[anonymus@parrot]-[-/Desktop/HomeworkLib] $python formula.py 5 3 -29.145 Canonymus@parrot-(-/Desktop/HomeworkLib] Solutions

1 import sys GAWN try: vo=eval (sys.argv[1]) 5 t=eval(sys.argv[2]). 6 except IndexError: t=eval (raw_input("t=")) 8 g=9.81 9 y=vo*t-0.5*g*t**2 10 print(y)

LUNUITYu JUULIUL LIDLJICOP CUCYy - $python formula.py 3 argi t=0.6 0.0342 IndexError: [anonymus@parrot]-[-/Desktop/HomeworkLib]) $ 3D

import sys try: Vo=eval(sys.argv[1]) t=eval(sys.argv[2]) except IndexError: t=eval (raw input("t=")), g=9.81 if(0<=t<=2*vo*g): print("ok") else: print("not valid") exit(0) y=vo*t-0.5*g*t**2 print(y)

L $python formula.py 3 t=6 ok y VotSgt 2 - 158.58int(y) (anonymus@parrot -[-/Desktop/HomeworkLib. -- $python formula.py 3 t=88 not valid Canonymus@parrot]-[-/Desktop/HomeworkLib]

1 Imp import sys 2 vo=eval (sys.argv[1]) 3 u=eval (sys.argv[2]) 4 g=9.81 5 Vo=v0*18/5 #convertion from kmph to mps 6 d=(1/2) *v0*2/(u*g) 7 print(d)

lanonymus@parrot-r-/Desktop/HomeworkLib. $python3 formula.py 120 0.3 146.78899082568807 [anonymus@parrot -[-/Desktop/HomeworkLib) $python3 formula.py 500.3 61.162079510703364 [anonymus@parrot]-[-/Desktop/HomeworkLib] - $ de CD Vo (ug) (anonymus@parrot]--/Desktop/HomeworkLib.

Add a comment
Know the answer?
Add Answer to:
Python Please. a)Let a program store the result of applying the eval function to the first...
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
  • Write a Python program that reads user input from the command-line. The program should define a...

    Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • . Use what you learned from our First Python Program to write a Python program that...

    . Use what you learned from our First Python Program to write a Python program that will carry out the following tasks. Ask user provide the following information: unit price (for example: 2.5) quantity (for example: 4) Use the following formula to calculate the revenue: revenue = unit price x quantity Print the result in the following format on the console: The revenue is $___. (for example: The revenue is $10.0.) . . Use the following formula to calculate the...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use...

    PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use IDLE to type, compile, and run (execute) the following programs. Name and save each program using the class name. Save all programs in one folder, call it Lab1-Practices. Save all programs for future reference as they illustrate some programming features and syntax that you may use in other labs and assignments. ================================== Program CountDown.py =================================== # Program CountDown.py # Demonstrate how to print to...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

  • PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for...

    PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for the user's most challenging course at Wilmington University. The second prompt should ask for the user's second most challenging course. The red boxes indicate the input from the user. Your program should respond with two copies of an enthusiastic comment about both courses. Be sure to include the input provided by the user to display the message. There are many ways to display a...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes...

    Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes a string argument from the command line and outputs the string in reversed order You have two constraints: Constraint 1: Each process can output at most one character. If you want to output more than a single character, you must fork off one or more processes in order to do that, and each of the forked processes in turn outputs a single character t...

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