I am trying to make a robot move (theoretically), some of my code does not work in python. I am getting some errors. How can I fix it?
This is my code:
#!/usr/bin/env python.3
import time
import sys
import random
#Provide a Menu for the User
def main():
print("To control LED press 1")
print("To drive press 2")
print("To change motor speed press 3")
print("To control the Servo press 4")
while True:
option = int(input("Enter the numbers 1, 2, 3, or 4:"))
if option == 1:
LED()
elif option == 2:
Drive()
elif option == 3:
Speed()
elif option == 4:
Servo()
else:
print("Wrong letter or number, please try again.")
#Options for LED
def LED():
print("To turn led on press 5")
print("To turn led off press 6")
print("To exit press 0")
while True:
option = int(input("Enter numbers 5 or 6:"))
if option == 5:
led_on(0)
elif option == 6:
led_off(0)
elif option == 0:
main()
#Options for Drive control
def Drive():
print("To move forward press W")
print("To move right press D")
print("To turn left press A")
print("To move backwards press S")
print("To increase speed press T")
print("To decrease speed press G")
print("To stop press X")
print("To exit press Z")
while True:
option = input("Enter letters W , D, A, S, T, G, X, or Z:")
if option == "W":
fwd()
time.sleep(.3)
elif option == "D":
right()
time.sleep(.3)
elif option == "A":
left()
time.sleep(.3)
elif option == "S":
backwards()
time.sleep(.3)
elif option == "T":
increase()
time.sleep(.3)
elif option == "G":
decrease()
time.sleep(.3)
elif option == "X":
stop()
elif option == "Z":
main()
#Options for Motor speed functions
def Speed():
print("Set Speed press 7")
print("To exit press 0")
while true:
option = int(input("Enter number 7:"))
if option == 7:
Set_speed()
elif option == 0:
main()
#Options to control Servo
def Servo():
print("To move servo to the right press 8")
print("To move servo to the left press 9")
print("To move servo forward press 10")
print("To return to the menu press 0")
while True:
option = int(input("Enter numbers 8, 9, or 10:"))
if option == 8:
enable_servo()
servo(0)
time.sleep(1)
disable_servo()
elif option == 9:
enable_servo()
servo(180)
time.sleep(1)
disable_servo()
elif option == 10:
enable_servo()
servo(90)
time.sleep(1)
disable_servo()
elif option == 0:
main()
main()
In: Computer Science
Write the code of:
– an abstract class called Creature
– classes Player and Monster (derived from Creature)
– classes WildPig and Dragon (derived from Monster)
In the Creature class
– Define a string member, Creature Name, to store the class
Creature’s name.(name should be dynamically allocated)
– Define Two virtual pure functions
• void DoAction() : Print the action of the object, and the actions
have to be different from different classes.
• void DrawOnScreen() : Print the object’s name and call DoAction()
belonging to the same class.
The class definition of Creature is:
Question: Implement the class Player, Monster, Dragon and Wildpig
so that when execution the following code, the counsel shows the
execution result as the following:
Counsel output:
Player <Kick_Ass> is attacking!!
Monster<UFO> is doing monster stuff!!
WildPig <I'm_Hungry> is Running!!
Dragon<I'm_the_Boss> is breathing Fire!!
Main function:
//==================================================
int main(){
Player hero("Kick_Ass");
Monster mon("UFO");
WildPig pig("I'm_Hungry");
Dragon drag("I'm_the_Boss");
Creature* object[4];
object[0]=&hero;
object[1]=&mon;
object[2]=&pig;
object[3]=&drag;
object[0]->DrawOnScreen();
object[1]->DrawOnScreen();
object[2]->DrawOnScreen();
object[3]->DrawOnScreen();
return0;
}
c++
In: Computer Science
Create program in Python (using import turtle):
1)includes draw_background function
2)draw_background function uses a loop to draw a background from at least one shape
3)uses at least 2 turtles to draw scent
4)includes draw_shape function
5)includes function to draw primary object
6)includes main method which creates the drawing
In: Computer Science
How do you translate this pseudocode to regular code in C++?
for i :1 to length(A) - 1
j = i
while j > 0 and A[j - 1] > A[j]
swap A[j] and A[j - 1]
j = j -1
In: Computer Science
The following functions have zero or more security issues given the way they are called in the main() function. Identity these security issues and propose a compliant fix. You don’t need to consider the issues in the main() function.
1. setValue set the element at index in arr to value.
void setValue ( int * arr , int len , int value , int index ) {
arr [ index ] = value ;
}
int main ( void ) {
int arr [10];
int value ;
int index ;
printf (" Enter the value :\ n ");
scanf ("% d " , & value );
printf (" Enter the position :\ n ");
scanf ("% d " , & index );
setValue ( arr , 10 , value , index );
return 0;
}
2. createArray creates an int array of size len. It, then, initializes each element in the array with value.
int * createArray ( int len , int value ) {
int * arr = ( int *) malloc ( sizeof ( int ) * len );
memset ( arr , value , sizeof ( int ) * len );
return arr ;
}
int main ( void ) {
int value ;
int len ;
printf (" Enter the value :\ n ");
scanf ("% d " , & value );
printf (" Enter the length :\ n ");
scanf ("% d " , & len );
int * arr = createArray ( len , value );
if ( arr != NULL ) {
if ( len > 0) {
printf (" The first element is % d \ n " , arr [0]);
}
free ( arr );
}
return 0;
}
3. writeToFile asks the user to input a string and then write the entered string to “out.txt”.
void writeToFile () {
char buffer [20];
printf (" Enter the content :\ n ");
scanf ("% s " , buffer );
FILE * f = fopen (" out . txt " , " w ");
fputs ( buffer );
fclose ( f );
}
int main ( void ) {
writeToFile ();
return 0;
}
4. sum returns the sum of two integers.
long long sum ( int a , int b ) {
return a + b ;
}
int main ( void ) {
int a = 0;
int b = 0;
printf (" Enter a :\ n ");
scanf ("% d " , & a );
printf (" Enter b :\ n ");
scanf ("% d " , & b );
printf (" sum of a and b is % ld \ n " , sum (a , b ));
return 0;
}
5. swap swaps the integer value stored in a and b.
void swap ( int * a , int * b ) {
* a += * b ;
* b = * a - * b ;
* a = * a - * b ;
}
int main ( void ) {
int a = 0;
int b = 0;
printf (" Enter a :\ n ");
scanf ("% d " , & a );
printf (" Enter b :\ n ");
scanf ("% d " , & b );
swap (& a , & b );
printf (" a is %d , b is % d \ n " , a , b );
return 0;
}
In: Computer Science
Compare and explain the pros and cons of native, cross-platform and web mobile development ?
In: Computer Science
In: Computer Science
Please use c# and write a console application
Part 1) A small airline has just purchased a computer for its
newly automated reservations system. You have been asked to develop
a new system. You are to write an application to assign seats on
each flight of the airline’s only plane. The plane has ten rows and
each row has 2 seats (total capacity: 20 seats). The first five
rows are for first class passengers, while the remaining rows are
for economy passengers. The application is used by an airline
employee who is given the task of assigning all seats to passengers
(one passenger at a time). Your application should display the
following alternatives: Please type 1 for First Class and Please
type 2 for Economy or type 3 to see the status of all seats. If the
user types 1, your application should assign a seat in the
first-class section (rows 1–5). If the user types 2, your
application should assign a seat in the economy section (rows
6–10).
If the user types 3, the application will display the status of all
seats (“A” for available and “X” for booked) as shown below. The
application will continue to ask for the user’s input until the
flight is full or the user suggests that they do not want to assign
anymore seats.
Availability Status:
X X
X X
X A
A A
A A
X X
X X
X X
X X
A A
Your application should never assign a seat that has already been assigned. When the economy section is full, your application should ask the person if it is acceptable to be placed in the first-class section (and vice versa). If yes, make the appropriate seat assignment. If no, display the message "Next flight leaves in 3 hours." You will need to use a multi-dimensional array to solve this problem.
Part 2) Use a one-dimensional array to solve the following
problem: A company pays its salespeople on a commission basis. The
salespeople receive $200 per week plus 9% of their gross sales for
that week. For example, a salesperson who grosses $5000 in sales in
a week receives $200 plus 9% of $5000, or a total of $650. Write an
application (using an array of counters) that determines how many
of the salespeople earned salaries in each of the following ranges
(assume that each salesperson’s salary is truncated to an integer
amount):
a) $200–299
b) $300–399
c) $400–499
d) $500–599
e) $600–699
f) $700–799
g) $800–899
h) $900–999
i) $1000 and over
The application will accept sales amount until the user enters -1
to end (as shown in figure 2.1 below). At this point, the
application calculates the salary and updates the appropriate
counter of the array. For instance, if the salesperson gets a total
salary of $565, you will update the counter that is counting totals
between the $500-$599 range.
Figure 2.1. Receiving input from user
Note that you do NOT need to store the individual salary of the salesperson. Summarize the results in tabular format Use only a for loop to display the output.
Final output showing all ranges and the number of agents that
received commission in that range
In: Computer Science
Python
The following dictionary contains the list of US states with their abbreviations as keys and full names as values:
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AS': 'American Samoa',
'AZ': 'Arizona',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DC': 'District of Columbia',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'GU': 'Guam',
'HI': 'Hawaii',
'IA': 'Iowa',
'ID': 'Idaho',
'IL': 'Illinois',
'IN': 'Indiana',
'KS': 'Kansas',
'KY': 'Kentucky',
'LA': 'Louisiana',
'MA': 'Massachusetts',
'MD': 'Maryland',
'ME': 'Maine',
'MI': 'Michigan',
'MN': 'Minnesota',
'MO': 'Missouri',
'MP': 'Northern Mariana Islands',
'MS': 'Mississippi',
'MT': 'Montana',
'NA': 'National',
'NC': 'North Carolina',
'ND': 'North Dakota',
'NE': 'Nebraska',
'NH': 'New Hampshire',
'NJ': 'New Jersey',
'NM': 'New Mexico',
'NV': 'Nevada',
'NY': 'New York',
'OH': 'Ohio',
'OK': 'Oklahoma',
'OR': 'Oregon',
'PA': 'Pennsylvania',
'PR': 'Puerto Rico',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VA': 'Virginia',
'VI': 'Virgin Islands',
'VT': 'Vermont',
'WA': 'Washington',
'WI': 'Wisconsin',
'WV': 'West Virginia',
'WY': 'Wyoming'
}
Write a program that randomly displays an abbreviation from this list, and then asks the user to enter the full name that matches that abbreviation. Once the user guesses, let the user know whether they are correct or incorrect. If they are incorrect, display the correct answer to the user.
In: Computer Science
Write a Python program to take as input 5 birthdays from 5 users (1 each) and output them in chronological order. Dates should include the month and day (not year) in the format “June 6” as a single input per user.
In: Computer Science
In: Computer Science
Write a program in Java that asks a user for one integer between 1 and 10 (inclusively). Write a switch statement writes out the number in english (i.e. 1 is “one”, etc).
In: Computer Science
Encode Diffie Hellamn exchange in software by writing a small program that accepts the values of p and g, and randomly generates the secret numbers SA and SB, and derives the Diffie Hellman secret.
Test it on the following examples:
p = 11, g = 13
p = 7, g = 17
p = 17, g = 13
In: Computer Science
Java
3. Describe the divide-and-conquer search algorithm and explain its efficiency. Consider two different Split functions: Split = Lo, and Split = (Lo + Hi) / 2. Draw the trees of recursive calls. Assume that we are searching a large data base with 4 million items represented as an ordered array. How many probes into the list will the binary search require before finding the target or concluding that it cannot be found? Assume that it takes 1 microsecond to access an item, estimate the execution time of the binary search.
In: Computer Science
You have been approached by the Statistics Canada to create a C# program to calculate the average salary of people with university degrees, college diplomas, and high school diplomas. Using a while loop, you are to process salary data until the user indicates that you should stop (there could be 0 or more data values). For each person processed, the program must first input an education type (char edType) (‘U’ or ‘u’ for university degrees, ‘C’ or ‘c’ for college diplomas, and ‘H’ or ‘h’ for high school) and then a salary (double salaryData). The program stops accepting input when the user enters a ‘Q’ or ‘q’ for quit (use a sentinel value while loop). Your main data processing loop should be conditioned on the fact that the user has not signalled quit. It might look something like:
while(char.ToUpper(edType) != ‘Q’)
This implies that like any sentinel value loop, you must seed the loop (input a value) PRIOR to entering the loop. Inside the loop the salary data is entered and processed. Once the main loop terminates, the average salary for each of the three education types should be printed out. Be sure to print an error message if the user enters an invalid education type or a negative salary.
In: Computer Science