• We had to restore from a backup today after a failed software update. Backup was from 0000 EDT and restored it at 0800 EDT so we lost about 8hrs. Today is 07/20/2024. More info here.

A Python3 Program, Meant To Test My Growing Python Skills... :)

B

blackneos940

Guest
I started learning python a while ago.... So far, a few months later, I can do things like THIS.....

import re

import webbrowser

new = 0

print ("Hello, and welcome to the Test Initiative...")

name = input("First, what is your name?\n>>>")

print ("Ok, got it. Your name is", name, ".")

age = input("Be HONEST, now. How old are you?\n>>>")

print ("Ah, ok. You're", age, "years old.")

interests = input("Any special interests? Hobbies?\n>>>")

print ("Ok. So your areas of interest include", interests, "...")

if 1 != 2:

book = {"dat1":"data1","dat2":"data2","dat3":"data3","dat4":"data4","dat5":"data5"}

print (book)

question = input ("Are you ready to continue? (Yes or No)\n>>>")

if question == "Yes" or question == "yes" or question == "Y" or question == "y":

print ("Ah, I see..... So you haven't lost interest yet. Let's continue.")

elif question == "No" or question == "no" or question == "N" or question == "n":

print ("What: You don't want to continue...?")

else:

print ("What? I didn't understand that. Try again.")

Earthbound = input("Have you heard of Earthbound, for the SNES?\n>>>")

if Earthbound == "Yes" or Earthbound == "yes" or Earthbound == "Y" or Earthbound == "y":

print ("Ah! So you HAVE! Here's a list of the first FOUR towns!")

list = ["Onett","Twoson","Threed","Fourside"]

print (list)

elif Earthbound == "No" or Earthbound == "no" or Earthbound == "N" or Earthbound == "n":

print("Ah, I see..... I'd explain it, but it's easier if you look it up online, or in a book somewhere...")

zipcode = r"Zip: \s*\d\d\d\d\d"

addr = "1234 Eversfield Rd, Stokesdale, N.C.\n Zip: 27284"

print (re.search(zipcode, addr))

final_question = input("Which OS (Operating System) is your favorite?\n>>>")

if final_question == "Linux" or final_question == "linux":

url = ("http://www.linux.org")

webbrowser.open(url, new = new)

elif final_question == "FreeBSD" or final_question == "freebsd" or final_question == "Free BSD" or final_question == "free bsd" or final_question == "free BSD":

url = ("http://www.freebsd.org")

elif final_question == "OSX" or final_question == "osx":

url = ("http://www.apple.com/osx")

webbrowser.open(url, new = new)

elif final_question == "Windows" or final_question == "windows":

url = ("http://windows.microsoft.com/en-US/windows/home")

webbrowser.open(url, new = new)t
print ("Bye for now! :D")

Anyway, what do you think.....? :D It's Open Source, but I don't know why you'd wanr to adopt it..... :L
 


Is a long questionnaire, jeje, but, it's accepted, I am starting python just like you, but I think you could make some debugging and clear the code a little..

Regards...
 
Is a long questionnaire, jeje, but, it's accepted, I am starting python just like you, but I think you could make some debugging and clear the code a little..

Regards...
Ok then..... :D But in what ways.....? It's Open Source.... :D Tell me!..... ^^
 
Working with dictionaries helps you to clean and make shorter your code....

Regards...
So..... How do I mix that with a Dictionary.....? :( I guess I have an IDEA, like:

Dict = {question_answer:?}

Well, actually, I have no clue..... XD
 
So..... How do I mix that with a Dictionary.....? :( I guess I have an IDEA, like:

Dict = {question_answer:?}

Well, actually, I have no clue..... XD

Thinking better, I'd like to use a tuple, For example:
answer("yes", "Yes", "Y", "y", "No", "no", "N", "n")
 
Thanks for posting this example for fellow noobies. I've been interested in learning a programming language for a while, but keep stalling because my life is to busy with other things.

I recently installed Zim Wiki as an effort to keep organized notes for myself as I study these things. :p
 
Code:
__author__ = 'ryanvade'
# Program to be run on the raspberry pi
import serial, time, os, sys, pprint, errno, csv, curses

# is this an Arm system (raspberry pi)
if not os.uname()[4].startswith("arm"):
    sys.stdout.write("Cannot run on: ")
    print(os.uname()[4])
    sys.exit(1)

#Is the RPi module available?
try:
    import RPi.GPIO as GPIO
except ImportError as e:
    print(e)
    sys.exit(1)
#Is the nanpy module available?
try:
    import nanpy
    from nanpy import (Arduino, OneWire, Lcd, SerialManager, ArduinoApi, Stepper, Servo)
except ImportError as e:
    print(e)
    sys.exit(1)


#variables
pp = pprint.PrettyPrinter(indent=4)
tty = "/dev/ttyAMA0"
connectionName = SerialManager(device=tty)
uno = ArduinoApi(connection=connectionName)
low = uno.LOW
high = uno.HIGH
message = " "
screen = curses.initscr()
defaultspeed = 127
currentspeed = defaultspeed
veercorrection = 39
decreasespeedvalue = 5
increasespeedvalue = 5
motor1PWM = 5
motor3PWM = 6
motor2PWM = 9
motor4PWM = 3


GND1 = 12
GND2 = 11

dir1 = 4
dir2 = 2
dir3 = 7
dir4 = 10



def stop():
    uno.digitalWrite(motor1PWM, 0)
    uno.digitalWrite(motor2PWM, 0)
    uno.digitalWrite(motor3PWM, 0)
    uno.digitalWrite(motor4PWM, 0)


def forward():
    uno.digitalWrite(dir1, high)
    uno.digitalWrite(dir3, high)
    uno.digitalWrite(dir2, low)
    uno.digitalWrite(dir4, low)


def left():
    uno.digitalWrite(dir1, low)
    uno.digitalWrite(dir3, high)
    uno.digitalWrite(dir2, high)
    uno.digitalWrite(dir4, low)


def right():
    uno.digitalWrite(dir1, high)
    uno.digitalWrite(dir3, low)
    uno.digitalWrite(dir2, low)
    uno.digitalWrite(dir4, high)


def reverse():
    uno.digitalWrite(dir1, low)
    uno.digitalWrite(dir3, low)
    uno.digitalWrite(dir2, high)
    uno.digitalWrite(dir4, high)


def setspeed(speed):
    if (speed >= 0) & (speed <= 255):
        uno.analogWrite(motor1PWM, speed - veercorrection)
        uno.analogWrite(motor2PWM, speed - veercorrection)
        uno.analogWrite(motor3PWM, speed)
        uno.analogWrite(motor4PWM, speed)
    else:
        print("Bad speed value")


def setleftspeed(speed):
    if (speed >= 0) & (speed <= 255):
        uno.analogWrite(motor1PWM, speed)
        uno.analogWrite(motor2PWM, speed)
    else:
        print("Bad speed value")


def smoothleft(speedleft, speedright):
    uno.analogWrite(motor1PWM, speedleft)
    uno.analogWrite(motor2PWM, speedleft)
    uno.analogWrite(motor3PWM,speedright)
    uno.analogWrite(motor4PWM, speedright)
    forward()


def smoothright(speedleft, speedright):
    uno.analogWrite(motor1PWM, speedleft)
    uno.analogWrite(motor2PWM, speedleft)
    uno.analogWrite(motor3PWM,speedright)
    uno.analogWrite(motor4PWM, speedright)
    forward()

stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)

stdscr.addstr(0, 10, "Hit 'q' to quit")
stdscr.refresh()

key = ''
while key != ord('q'):
    key = stdscr.getch()
    stdscr.addch(20,25,key)
    stdscr.refresh()
    setspeed(currentspeed)

    if key == curses.KEY_UP:
        stdscr.addstr(2, 20, "Up")
        forward()
    elif key == curses.KEY_DOWN:
        stdscr.addstr(3, 20, "Down")
        reverse()
    elif key == curses.KEY_LEFT:
        stdscr.addstr(4, 20, "LEFT")
        left()
    elif key == curses.KEY_RIGHT:
        stdscr.addstr(5, 20, "RIGHT")
        right()
    elif key == curses.KEY_NPAGE:
        stdscr.addstr(6 , 20, "Next Page")
        currentspeed = currentspeed + increasespeedvalue
        setspeed(currentspeed)
    elif key == curses.KEY_PPAGE:
        stdscr.addstr(7, 20, "PREVIOUS Page")
        currentspeed = currentspeed - decreasespeedvalue
        setspeed(currentspeed)
    elif key == ord("s"):
        stdscr.addstr(8, 20, "s")
        stop()

curses.endwin()
stop()
 
Code:
__author__ = 'ryanvade'
# Program to be run on the raspberry pi
import serial, time, os, sys, pprint, errno, csv, curses

# is this an Arm system (raspberry pi)
if not os.uname()[4].startswith("arm"):
    sys.stdout.write("Cannot run on: ")
    print(os.uname()[4])
    sys.exit(1)

#Is the RPi module available?
try:
    import RPi.GPIO as GPIO
except ImportError as e:
    print(e)
    sys.exit(1)
#Is the nanpy module available?
try:
    import nanpy
    from nanpy import (Arduino, OneWire, Lcd, SerialManager, ArduinoApi, Stepper, Servo)
except ImportError as e:
    print(e)
    sys.exit(1)


#variables
pp = pprint.PrettyPrinter(indent=4)
tty = "/dev/ttyAMA0"
connectionName = SerialManager(device=tty)
uno = ArduinoApi(connection=connectionName)
low = uno.LOW
high = uno.HIGH
message = " "
screen = curses.initscr()
defaultspeed = 127
currentspeed = defaultspeed
veercorrection = 39
decreasespeedvalue = 5
increasespeedvalue = 5
motor1PWM = 5
motor3PWM = 6
motor2PWM = 9
motor4PWM = 3


GND1 = 12
GND2 = 11

dir1 = 4
dir2 = 2
dir3 = 7
dir4 = 10



def stop():
    uno.digitalWrite(motor1PWM, 0)
    uno.digitalWrite(motor2PWM, 0)
    uno.digitalWrite(motor3PWM, 0)
    uno.digitalWrite(motor4PWM, 0)


def forward():
    uno.digitalWrite(dir1, high)
    uno.digitalWrite(dir3, high)
    uno.digitalWrite(dir2, low)
    uno.digitalWrite(dir4, low)


def left():
    uno.digitalWrite(dir1, low)
    uno.digitalWrite(dir3, high)
    uno.digitalWrite(dir2, high)
    uno.digitalWrite(dir4, low)


def right():
    uno.digitalWrite(dir1, high)
    uno.digitalWrite(dir3, low)
    uno.digitalWrite(dir2, low)
    uno.digitalWrite(dir4, high)


def reverse():
    uno.digitalWrite(dir1, low)
    uno.digitalWrite(dir3, low)
    uno.digitalWrite(dir2, high)
    uno.digitalWrite(dir4, high)


def setspeed(speed):
    if (speed >= 0) & (speed <= 255):
        uno.analogWrite(motor1PWM, speed - veercorrection)
        uno.analogWrite(motor2PWM, speed - veercorrection)
        uno.analogWrite(motor3PWM, speed)
        uno.analogWrite(motor4PWM, speed)
    else:
        print("Bad speed value")


def setleftspeed(speed):
    if (speed >= 0) & (speed <= 255):
        uno.analogWrite(motor1PWM, speed)
        uno.analogWrite(motor2PWM, speed)
    else:
        print("Bad speed value")


def smoothleft(speedleft, speedright):
    uno.analogWrite(motor1PWM, speedleft)
    uno.analogWrite(motor2PWM, speedleft)
    uno.analogWrite(motor3PWM,speedright)
    uno.analogWrite(motor4PWM, speedright)
    forward()


def smoothright(speedleft, speedright):
    uno.analogWrite(motor1PWM, speedleft)
    uno.analogWrite(motor2PWM, speedleft)
    uno.analogWrite(motor3PWM,speedright)
    uno.analogWrite(motor4PWM, speedright)
    forward()

stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)

stdscr.addstr(0, 10, "Hit 'q' to quit")
stdscr.refresh()

key = ''
while key != ord('q'):
    key = stdscr.getch()
    stdscr.addch(20,25,key)
    stdscr.refresh()
    setspeed(currentspeed)

    if key == curses.KEY_UP:
        stdscr.addstr(2, 20, "Up")
        forward()
    elif key == curses.KEY_DOWN:
        stdscr.addstr(3, 20, "Down")
        reverse()
    elif key == curses.KEY_LEFT:
        stdscr.addstr(4, 20, "LEFT")
        left()
    elif key == curses.KEY_RIGHT:
        stdscr.addstr(5, 20, "RIGHT")
        right()
    elif key == curses.KEY_NPAGE:
        stdscr.addstr(6 , 20, "Next Page")
        currentspeed = currentspeed + increasespeedvalue
        setspeed(currentspeed)
    elif key == curses.KEY_PPAGE:
        stdscr.addstr(7, 20, "PREVIOUS Page")
        currentspeed = currentspeed - decreasespeedvalue
        setspeed(currentspeed)
    elif key == ord("s"):
        stdscr.addstr(8, 20, "s")
        stop()

curses.endwin()
stop()


Lol..... XD Showoff.....! :D There are n00bz here, such as myself..... XD But no, seriously, thank you for the help..... :) I'll study it, since I've gotten better, and I should be able understand it....... :D
 
Thanks for posting this example for fellow noobies. I've been interested in learning a programming language for a while, but keep stalling because my life is to busy with other things.

I recently installed Zim Wiki as an effort to keep organized notes for myself as I study these things. :p
Really.....? I thought you were WAY better than I..... :D After all, while this site is newbie-friendly, it can get pretty technical at times..... ;)
 
Thanks for posting this example for fellow noobies. I've been interested in learning a programming language for a while, but keep stalling because my life is to busy with other things.

I recently installed Zim Wiki as an effort to keep organized notes for myself as I study these things. :p

Btw, have I really helped you.....? :) If so, then it made my day..... :3
 
It helps just to see some of the code structure or see how similar or different it is to BASH.


Ok.....! :) Oh, here's a basic Web Browser..... :3


import webbrowser

new = 0

url = input("Which Website Would ya' like to visit? :3\nEnter the URL here, and don't forget the http://! :D Example: http://www.example.com .")

webbrowser.open(url, new = new)
 

Members online


Top