diff --git a/challenges146_150/challenge-146_.py b/challenges146_150/challenge-146_.py new file mode 100644 index 0000000..4152396 --- /dev/null +++ b/challenges146_150/challenge-146_.py @@ -0,0 +1,57 @@ +alphabet=["a", "b", "c", "d", "e", "f", "g", "h", "i", +"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", +"u", "v", "w", "x", "y", "z", " "] + +def get_data(): + word=input("Enter your message: ") + word=word.lower() + num=int(input("Enter a number (1-26): ")) + while num>26 or num==0: + num=int(input("Out of range, Please enter a number (1-26): ")) + data=(word,num) + return(data) + +def make_code(word,num): + new_word="" + for x in word: + y=alphabet.index(x) + y=y+num + if y>26: + y=y-27 + char=alphabet[y] + new_word=new_word +char + print(new_word) + print() + +def decode(word,num): + new_word="" + for x in word: + y=alphabet.index(x) + y=y-num + if y<0: + y=y+27 + char=alphabet[y] + new_word=new_word +char + print(new_word) + print() + +def main(): + again=True + while again==True: + print("1) Make a code") + print("2) Decode a message") + print("3) Quit") + print() + selection=int(input("Enter your selection: ")) + if selection==1: + (word,num)=get_data() + make_code(word,num) + elif selection==2: + (word,num)=get_data() + decode(word,num) + elif selection==3: + again=False + else: + print("Incorrect selection") + +main() diff --git a/challenges146_150/challenge-147_.py b/challenges146_150/challenge-147_.py new file mode 100644 index 0000000..8d787ce --- /dev/null +++ b/challenges146_150/challenge-147_.py @@ -0,0 +1,92 @@ +import random + +def select_col(): + colours=["r", "b", "o", "y", "p", "g", "w"] + c1=random.choice(colours) + c2=random.choice(colours) + c3=random.choice(colours) + c4=random.choice(colours) + data=(c1, c2, c3, c4) + return data + +def tryit(c1, c2, c3, c4): + print("The colours are: (r)ed, (b)lue, (o)range, (y)ellow, (p)ink, (g)reen, (w)hite") + try_again=True + while try_again==True: + u1=input("Enter your choice for place 1: ") + u1=u1.lower() + if u1 !="r" and u1 !="b" and u1 !="o" and u1 !="y" and u1 !="p" and u1 !="g" and u1 !="w": + print("Incorrect selection") + else: + try_again=False + + try_again=True + while try_again==True: + u2=input("Enter your choice for place 2: ") + u2=u2.lower() + if u2 !="r" and u2 !="b" and u2 !="o" and u2 !="y" and u2 !="p" and u2 !="g" and u2 !="w": + print("Incorrect selection") + else: + try_again=False + + try_again=True + while try_again==True: + u3=input("Enter your choice for place 3: ") + u3=u3.lower() + if u3 !="r" and u3 !="b" and u3 !="o" and u3 !="y" and u3 !="p" and u3 !="g" and u3 !="w": + print("Incorrect selection") + else: + try_again=False + + try_again=True + while try_again==True: + u4=input("Enter your choice for place 4: ") + u4=u4.lower() + if u4 !="r" and u4 !="b" and u4 !="o" and u4 !="y" and u4 !="p" and u4 !="g" and u4 !="w": + print("Incorrect selection") + else: + try_again=False + + correct=0 + wrong_place=0 + + if c1==u1: + correct=correct+1 + elif c1==u2 or c1==u3 or c1==u4: + wrong_place=wrong_place+1 + + if c2==u2: + correct=correct+1 + elif c2==u1 or c2==u3 or c2==u4: + wrong_place=wrong_place+1 + + if c3==u3: + correct=correct+1 + elif c3==u1 or c3==u2 or c3==u4: + wrong_place=wrong_place+1 + + if c4==u4: + correct=correct+1 + elif c4==u1 or c4==u2 or c4==u3: + wrong_place=wrong_place+1 + + print("Correct colour in the correct place: ", correct) + print("Correct colour but in the wrong place: ", wrong_place) + print() + + data2=(correct, wrong_place) + return data2 + +def main(): + (c1, c2, c3, c4) = select_col() + score = 0 + play = True + while play == True: + (correct, wrong_place) = tryit(c1, c2, c3, c4) + score=score+1 + if correct==4: + play=False + print("You win!") + print("You took", score, "guesses") + +main() \ No newline at end of file diff --git a/challenges146_150/challenge-148_.py b/challenges146_150/challenge-148_.py new file mode 100644 index 0000000..593dde4 --- /dev/null +++ b/challenges146_150/challenge-148_.py @@ -0,0 +1,144 @@ +import csv + +def get_data(): + file=list(csv.reader(open("passwords.csv"))) + tmp=[] + for x in file: + tmp.append(x) + return tmp + +def create_userID(tmp): + name_again=True + while name_again==True: + userID=input("Enter a new user ID: ") + userID.lower() + inlist=False + row=0 + for y in tmp: + if userID in tmp[row][0]: + print(userID, "has already been allocated") + inlist=True + row=row+1 + if inlist==False: + name_again=False + return userID + +def create_password(): + sclist=["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "?"] + nclist=["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] + tryagain=True + while tryagain==True: + score=0 + uc=False + lc=False + sc=False + nc=False + password=input("Enter Password: ") + password2=input("Enter password again: ") + length=len(password) + if length>=8: + score=score+1 + for x in password: + if x.islower(): + lc=True + if x.isupper(): + uc=True + if x in sclist: + sc=True + if x in nclist: + ns=True + + if sc==True: + score=score+1 + if lc==True: + score=score+1 + if uc==True: + score=score+1 + if nc==True: + score=score+1 + + if score==1 or score==2: + print("This is a weak password. Try again.") + + if score==3 or score==4: + print("This password could be improved. ") + again=input("Do you want to try for a stronger password? (y/n) ") + again.lower() + if again=="n": + tryagain=False + + if password != password2: + print("Password do not match. File not saved") + main() + else: + return password + +def find_userID(tmp): + ask_name_again=True + userID="" + while ask_name_again == True: + searchID=input("Enter the user ID you are looking for ") + searchID.lower() + inlist=False + row=0 + for y in tmp: + if searchID in tmp[row][0]: + inlist=True + row=row+1 + if inlist==True: + userID=searchID + ask_name_again=False + else: + print(searchID, "is NOT in the list") + return userID + +def change_password(userID,tmp): + if userID!="": + password=create_password() + ID=userID.index(userID) + tmp(ID)[1]=password + file=open("passwords.csv", "w") + x=0 + for row in tmp: + newrecord=tmp[x][0] + "," +tmp[x][1] + "\n" + file.write(newrecord) + x=x+1 + file.close() + +def display_all_userID(): + tmp=get_data() + x=0 + for row in tmp: + print(tmp[x][0]) + x=x+1 + +def main(): + tmp=get_data() + go_again=True + while go_again==True: + print() + print("1) Create a new User ID") + print("2) Change a password") + print("3) Display all User IDs") + print("4) Quit") + print() + + selection=int(input("Enter Selection: ")) + if selection==1: + userID=create_userID(tmp) + password=create_password() + file=open("Passwords.csv","a") + newrecord=userID+","+password+"\n" + file.write(str(newrecord)) + file.close() + elif selection==2: + userID=find_userID(tmp) + change_password(userID,tmp) + elif selection==3: + display_all_userID() + elif selection==4: + go_again=False + else: + print("Incorrect selection") + +main() \ No newline at end of file diff --git a/challenges146_150/challenge-149_.py b/challenges146_150/challenge-149_.py new file mode 100644 index 0000000..0653a10 --- /dev/null +++ b/challenges146_150/challenge-149_.py @@ -0,0 +1,38 @@ +from tkinter import * + +def show_table(): + num=int(num_box.get()) + value=1 + for i in range (1,13): + answer=i*num + num_list.insert(END,(i, "x", num, "=", answer)) + value=value+1 + num_box.delete(0,END) + num_box.focus() + +def clear_list(): + num_box.delete(0,END) + num_list.delete(0,END) + num_box.focus() + +window=Tk() +window.title=("Times Table") +window.geometry("400x280") + +label1=Label(text="Enter a number: ") +label1.place(x=20, y=20, width=100, height=25) + +num_box=Entry(text=0) +num_box.place(x=120, y=20, width=100, height=25) +num_box.focus() + +button1=Button(text="View Times Table", command=show_table) +button1.place(x=250, y=20, width=120, height=25) + +num_list=Listbox() +num_list.place(x=120, y=50, width=100, height=200) + +button2=Button(text="Clear", command=clear_list) +button2.place(x=250, y=50, width=120, height=25) + +window.mainloop() \ No newline at end of file diff --git a/challenges146_150/challenge-150_.py b/challenges146_150/challenge-150_.py new file mode 100644 index 0000000..3804ba0 --- /dev/null +++ b/challenges146_150/challenge-150_.py @@ -0,0 +1,230 @@ +import sqlite3 +from tkinter import * + +with sqlite3.connect("Art.db") as db: + cursor=db.cursor() + +cursor.execute("""CREATE TABLE IF NOT EXISTS Artists(artistid integer PRIMARY KEY, name text, address text, town text, county text, postcode text); +""") + +cursor.execute("""CREATE TABLE IF NOT EXISTS Art(pieceid integer PRIMARY KEY, artistid integer, title text, medium text, price integer); +""") + +def addartist(): + newname=artistname.get() + newaddress=artistadd.get() + newtown=artisttown.get() + newcounty=artistcounty.get() + newpostcode=artistpostcode.get() + cursor.execute("""INSERT INTO Artists(name, address, town, county, postcode) + VALUES (?,?,?,?,?)""",(newname, newaddress, newtown, newcounty, newpostcode)) + db.commit() + artistname.delete(0,END) + artistadd.delete(0,END) + artisttown.delete(0,END) + artistcounty.delete(0,END) + artistpostcode.delete(0,END) + artistname.focus() + +def clearartist(): + artistname.delete(0,END) + artistadd.delete(0,END) + artisttown.delete(0,END) + artistcounty.delete(0,END) + artistpostcode.delete(0,END) + artistname.focus() + +def addart(): + newartname=artname.get() + newtitle=arttitle.get() + newmedium=medium.get() + newprice=artprice.get() + cursor.execute("""INSERT INTO Art(artistid, title, medium, price) + VALUES (?,?,?,?)""",(newartname, newtitle, newmedium, newprice)) + db.commit() + artname.delete(0,END) + arttitle.delete(0,END) + medium.set("") + artprice.delete(0,END) + artistname.focus() + +def clearart(): + artname.delete(0,END) + arttitle.delete(0,END) + medium.set("") + artprice.delete(0,END) + artistname.focus() + +def clearwindow(): + outputwindow.delete(0,END) + +def viewartists(): + cursor.execute("SELECT * FROM Artists") + for x in cursor.fetchall(): + newrecord=str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+","+x[5]+"\n" + outputwindow.insert(END,newrecord) + +def viewart(): + cursor.execute("SELECT * FROM Art") + for x in cursor.fetchall(): + newrecord=str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"\n" + outputwindow.insert(END,newrecord) + +def searchartistoutput(): + selectedartist=searchartist.get() + cursor.execute("SELECT name FROM Artists WHERE artistid=?",[selectedartist]) + for x in cursor.fetchall(): + outputwindow.insert(END,x) + cursor.execute("SELECT * FROM Art WHERE artistid=?",[selectedartist]) + for x in cursor.fetchall(): + newrecord=str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"\n" + outputwindow.insert(END,newrecord) + searchartist.delete(0,END) + searchartist.focus() + +def searchmediumoutput(): + selectedmedium=medium2.get() + cursor.execute("SELECT Art.pieceid, Artists.name, Art.title, Art.medium, Art.price FROM Artists, Art WHERE Artists.artistid=Art.artistid AND Art.medium=?",[selectedmedium]) + for x in cursor.fetchall(): + newrecord=str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"\n" + outputwindow.insert(END,newrecord) + medium2.set("") + +def searchbyprice(): + minprice=selectmin.get() + maxprice=selectmax.get() + cursor.execute("SELECT Art.pieceid, Artists.name, Art.title, Art.medium, Art.price FROM Artists, Art WHERE Artists.artistid=Art.artistid AND Art.price>=? AND Art.price<=?",[minprice,maxprice]) + for x in cursor.fetchall(): + newrecord=str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"\n" + outputwindow.insert(END,newrecord) + selectmin.delete(0,END) + selectmax.delete(0,END) + selectmin.focus() + +def sold(): + file=open("SoldArt.txt","a") + selectedpiece=soldpiece.get() + cursor.execute("SELECT * FROM Art WHERE pieceid=?",[selectedpiece]) + for x in cursor.fetchall(): + newrecord=str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"\n" + file.write(newrecord) + file.close() + cursor.execute("DELETE FROM Art WHERE pieceid=?",[selectedpiece]) + db.commit() + +window=Tk() +window.title("Art") +window.geometry("1220x600") + +title1=Label(text="Enter new details: ") +title1.place(x=10, y=10, width=100, height=25) + +artistnamelbl=Label(text="Name: ") +artistnamelbl.place(x=30, y=40, width=80, height=25) +artistname=Entry(text="") +artistname.place(x=110, y=40, width=200, height=25) +artistname.focus() + +artistaddlbl=Label(text="Address: ") +artistaddlbl.place(x=310, y=40, width=80, height=25) +artistadd=Entry(text="") +artistadd.place(x=390, y=40, width=200, height=25) + +artisttownlbl=Label(text="Town: ") +artisttownlbl.place(x=590, y=40, width=80, height=25) +artisttown=Entry(text="") +artisttown.place(x=670, y=40, width=100, height=25) + +artistcountylbl=Label(text="County: ") +artistcountylbl.place(x=770, y=40, width=80, height=25) +artistcounty=Entry(text="") +artistcounty.place(x=850, y=40, width=100, height=25) + +artistpostcodelbl=Label(text="Postcode: ") +artistpostcodelbl.place(x=950, y=40, width=80, height=25) +artistpostcode=Entry(text="") +artistpostcode.place(x=1030, y=40, width=100, height=25) + +addbtn=Button(text="Add Artist", command=addartist) +addbtn.place(x=110, y=80, width=130, height=25) + +clearbtn=Button(text="Clear Artist", command=clearartist) +clearbtn.place(x=250, y=80, width=130, height=25) + +artnamelbl=Label(text="Artist ID: ") +artnamelbl.place(x=30, y=120, width=80, height=25) +artname=Entry(text="") +artname.place(x=110, y=120, width=50, height=25) + +arttitlelbl=Label(text="Title: ") +arttitlelbl.place(x=200, y=120, width=80, height=25) +arttitle=Entry(text="") +arttitle.place(x=280, y=120, width=280, height=25) + +artmediumlbl=Label(text="Medium: ") +artmediumlbl.place(x=590, y=120, width=80, height=25) + +medium=StringVar(window) +artmedium=OptionMenu(window, medium, "Oil", "Watercolour", "Ink", "Acrylic") +artmedium.place(x=670, y=120, width=100, height=25) + +artpricelbl=Label(text="Price: ") +artpricelbl.place(x=770, y=120, width=80, height=25) +artprice=Entry(text="") +artprice.place(x=850, y=120, width=100, height=25) + +addartbtn=Button(text="Add Piece", command=addart) +addartbtn.place(x=110, y=150, width=130, height=25) + +clearartbtn=Button(text="Clear Piece", command=clearart) +clearartbtn.place(x=250, y=150, width=130, height=25) + +outputwindow=Listbox() +outputwindow.place(x=10, y=200, width=1000, height=350) + +clearoutputwindow=Button(text="Clear output", command=clearwindow) +clearoutputwindow.place(x=10, y=200, width=1000, height=350) + +viewallartists=Button(text="View All Artists", command=viewartists) +viewallartists.place(x=1020, y=230, width=155, height=25) + +viewallart=Button(text="View All Art", command=viewart) +viewallart.place(x=1020, y=260, width=155, height=25) + +searchartist=Entry(text="") +searchartist.place(x=1020, y=300, width=50, height=25) + +searchartistbtn=Button(text="Search by Artist", command=searchartistoutput) +searchartistbtn.place(x=1075, y=300, width=100, height=25) + +medium2=StringVar(window) + +searchmedium=OptionMenu(window, medium2, "Oil", "Watercolour", "Ink", "Acrylic") +searchmedium.place(x=1020, y=330, width=100, height=25) + +searchmediumbtn=Button(text="Search", command=searchmediumoutput) +searchmediumbtn.place(x=1125, y=330, width=50, height=25) + +minlbl=Label(text="Min: ") +minlbl.place(x=1020, y=360, width=75, height=25) + +maxlbl=Label(text="Max: ") +minlbl.place(x=1100, y=360, width=75, height=25) + +selectmin=Entry(text="") +selectmin.place(x=1020, y=380, width=75, height=25) + +selectmax=Entry(text="") +selectmax.place(x=1100, y=380, width=75, height=25) + +searchpricebtn=Button(text="Search by Price", command=searchbyprice) +searchpricebtn.place(x=1020, y=410, width=155, height=25) + +soldpiece=Entry(text="") +soldpiece.place(x=1020, y=450, width=50, height=25) + +soldbtn=Button(text="Sold", command=sold) +soldbtn.place(x=1075, y=450, width=100, height=25) + +window.mainloop() +db.close() \ No newline at end of file