Question

In: Computer Science

I am trying to make a robot move (theoretically), some of my code does not work...

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()

Solutions

Expert Solution

/****************************main.py*****************************/

#!/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()

I Found One syntax error with is in while true: so replace it with True

and may be indentation error...

Please let me know if you have any doubt or modify the answer , Thanks :)


Related Solutions

I am trying to make a new code that uses functions to make it. My functions...
I am trying to make a new code that uses functions to make it. My functions are below the code. <?php */ $input; $TenBills = 1000; $FiveBills = 500; $OneBills = 100; $Quarters = 25; $Dimes = 10; $Nickels = 5; $Pennies = 1; $YourChange = 0; $input = readline("Hello, please enter your amount of cents:\n"); if(ctype_digit($input)) { $dollars =(int)($input/100); $cents = $input%100;    $input >= $TenBills; $YourChange = (int)($input/$TenBills); $input -= $TenBills * $YourChange; print "Change for $dollars dollars...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
I am having an issue with trying to make my code to have a required output...
I am having an issue with trying to make my code to have a required output ////////////Input////////// With computer science, you can work in any industry. 0 //////////Output////////// Required Output Enter some text to encrypt\n Enter a key\n Error: Key is divisible by 26. That's a bad key!\n Useless key: 0\n ///////////Cipher.java///////// ------------ My code. public class Cipher { private String plainText; private int key; public Cipher(String text, int key) throws EmptyPlainText, UselessKeyException { if (text == null || text.length()...
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
this is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
1. I am trying to determine the level of measurement of my data type? I am...
1. I am trying to determine the level of measurement of my data type? I am looking for advice on Nominal, Ordinal, Interval, and Ratio 2. Does the data set have any categorical variables? I am trying to Describe the data set below in very general terms? This data consist of 8 variables: Which are GRE Scores, TOEFL Scores, University Rating, Statement of Purpose, Letter of Recommendation Strength, Undergraduate GPA, . Research Experience, and Chance of Admit. Name Type Description...
I am trying to start saving for retirement. I am investing all my cash into the...
I am trying to start saving for retirement. I am investing all my cash into the S&P 500, which will assume consistently 9.8% interest, compounded annually. I initially put a lump sum of $100 into my account, and I will deposit $10 every second week. a) After 10 years, how much money will I have invested? b) After 10 years, if I sold all of my stocks, how much money will I have in my account? c) After 25 years,...
I am working on a project for my Computer Science course. I am trying to create...
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT