global name 'rank' is not defined
I'm new to Python programming and have problems with this code. rank and
suit does not work in method shuffle. My "simple" (?) Question is, why?
from Tkinter import *
from Canvas import Rectangle, CanvasText, Group, Window
from PIL import Image
import ImageTk
win = Tk()
text = Text(win, width=65, height=15, font=("Arial", 10))
win.title("Play High or Low Card")
win.geometry("700x600")
class Card(object):
RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K"]
SUIT = ["Python/projekt/bilder/hearts.png",
"Python/projekt/bilder/spades.png",
"Python/projekt/bilder/diamond.png",
"Python/projekt/bilder/clubs.png"]
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
rep = self.rank + self.suit
return rep
def draw(self,suit,rank):
bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))
cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1,
relief='solid', highlightthickness=2)
cardGraph.photo=bg
cardGraph.create_image(10,10, image=bg, anchor=CENTER) #left/up
cardGraph.create_image(53,93, image=bg, anchor=CENTER) #right/down
cardGraph.create_text(20, 10, text=self.rank, font=("Helvetica",
8, "bold")) #left/up
cardGraph.create_text(63, 93, text=self.rank, font=("Helvetica",
8, "bold")) #right/down
cardGraph.create_text(36, 50, text=self.rank, font=("Helvetica",
12, "bold")) #middle
cardGraph.pack(side = "left", anchor=NW)
class Hand(object):
def __init__(self):
self.cards = []
def __str__ (self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + " "
else:
rep = "<empty>"
return rep
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
class Deck(Hand, Card):
def populate(self):
for suit in Card.SUIT:
for rank in Card.RANKS:
self.add(Card(rank, suit))
DrawCard = Card(rank,suit)
DrawCard.draw(self,rank)
def shuffle(self):
import random
random.shuffle(self.cards)
DrawCard = Card(rank,suit)
DrawCard.draw(self,rank)
def deal(self, hands, per_hand = 1):
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[1]
self.give(top_card, hand)
else:
print("Cant continue deck. Out of cards!!")
deck1 = Deck()
deck1.populate()
deck1.shuffle()
my_hand = Hand()
your_hand = Hand()
hands = [my_hand, your_hand]
deck1.deal(hands, per_hand = 5)
print(my_hand)
shuffleBtn = Button(win, text="Turn", command=lambda: deck1.shuffle())
shuffleBtn.pack()
mainloop()
The traceback is:
Traceback (most recent call last): File "C:/Users/RaJ/Desktop/Cards", line
95, in deck1.shuffle() File "C:/Users/RaJ/Desktop/Cards", line 77, in
shuffle DrawCard = Card(rank,suit) NameError: global name 'rank' is not
defined
Buske
Sunday, 1 September 2013
Saturday, 31 August 2013
JAVA dynamic threading
JAVA dynamic threading
i'm a begginer in java, and i have this code i made, but that t thread
gets only executed once, how can i make the thread be dynamic?
//main program - creating 2 threads, unfortuantely at this point only 1
it's running
public static void main(String[] args){
timeThread ttm = new timeThread();
ttm.name = "map";
ttm.min = 1000;
ttm.max = 5000;
ttm.start();
timeThread tta = new timeThread();
tta.name = "arena";
tta.min = 6000;
tta.max = 10000;
tta.start();
}
//the timethread i'm calling in the program
static class timeThread{
static String name;
static int min;
static int max;
static int random;
static Thread t = new Thread () {
public void run () {
while (true){
random = genRandomInteger(min,max);
System.out.println("Thread named: "
+ name + " running for: "
+ random + " secconds...");
try {
Thread.sleep(random);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
};
void start(){
t.start();
}
}
//the random function generator
private static int genRandomInteger(int aStart, int aEnd){
int returnValue = aStart + (int)(Math.random()
* ((aEnd - aStart) + 1));
return returnValue;
}
i'm a begginer in java, and i have this code i made, but that t thread
gets only executed once, how can i make the thread be dynamic?
//main program - creating 2 threads, unfortuantely at this point only 1
it's running
public static void main(String[] args){
timeThread ttm = new timeThread();
ttm.name = "map";
ttm.min = 1000;
ttm.max = 5000;
ttm.start();
timeThread tta = new timeThread();
tta.name = "arena";
tta.min = 6000;
tta.max = 10000;
tta.start();
}
//the timethread i'm calling in the program
static class timeThread{
static String name;
static int min;
static int max;
static int random;
static Thread t = new Thread () {
public void run () {
while (true){
random = genRandomInteger(min,max);
System.out.println("Thread named: "
+ name + " running for: "
+ random + " secconds...");
try {
Thread.sleep(random);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
};
void start(){
t.start();
}
}
//the random function generator
private static int genRandomInteger(int aStart, int aEnd){
int returnValue = aStart + (int)(Math.random()
* ((aEnd - aStart) + 1));
return returnValue;
}
Is this dependency injection?
Is this dependency injection?
A simple question that is perhaps a bit silly. Is this dependency
injection? I do not mean a DI container.
Foo depends on an instance of Bar. Foo gets an instance of Bar passed by
constructor and does not care even for the initialization.
public class Foo
{
public Foo(IBar bar)
{
DoSomething(bar);
}
private void DoSomething(IBar bar)
{
// ...
}
}
public class Bar : IBar
{
// ...
}
Is passing an instance of Bar to the constructor a dependency injection?
Especially constructor injection.
A simple question that is perhaps a bit silly. Is this dependency
injection? I do not mean a DI container.
Foo depends on an instance of Bar. Foo gets an instance of Bar passed by
constructor and does not care even for the initialization.
public class Foo
{
public Foo(IBar bar)
{
DoSomething(bar);
}
private void DoSomething(IBar bar)
{
// ...
}
}
public class Bar : IBar
{
// ...
}
Is passing an instance of Bar to the constructor a dependency injection?
Especially constructor injection.
DB_DataObject and PDO
DB_DataObject and PDO
I was told that using PDO is recommended to make my code more secure from
mysql injections.
I am currently using DB_DataObject that I read that cleans the input from
injections as well
(http://pear.php.net/manual/en/package.database.db-dataobject.php) Do I
still need to use PDO or DB_Dataobject should be ok ? Also can I combine
them together and if yes how.
Example part of my DB statement
$password=encryptpass($_REQUEST['password']);
$user->query("select username from {$user->__table} where
(username = '$username' or email='$username') AND password =
'$password' ");
Thanks
I was told that using PDO is recommended to make my code more secure from
mysql injections.
I am currently using DB_DataObject that I read that cleans the input from
injections as well
(http://pear.php.net/manual/en/package.database.db-dataobject.php) Do I
still need to use PDO or DB_Dataobject should be ok ? Also can I combine
them together and if yes how.
Example part of my DB statement
$password=encryptpass($_REQUEST['password']);
$user->query("select username from {$user->__table} where
(username = '$username' or email='$username') AND password =
'$password' ");
Thanks
Can't Figure out the Null Reference
Can't Figure out the Null Reference
Here is my code (places the form on top of ALL aplications:
Imports System.Runtime.InteropServices _ Private Shared Function
SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal
X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As
Integer, ByVal uFlags As Integer) As Boolean End Function
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Public Function MakeTopMost()
If SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE).Equals(True) Then
SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE)
End If
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
MakeTopMost()
End Sub
VS 2010 says "Function... doesn't return a value on all code paths. A null
reference exception could occur at run time when the result is used.
I have been at this for hours but annot figgure it out. Does anyone know?
Here is my code (places the form on top of ALL aplications:
Imports System.Runtime.InteropServices _ Private Shared Function
SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal
X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As
Integer, ByVal uFlags As Integer) As Boolean End Function
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Public Function MakeTopMost()
If SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE).Equals(True) Then
SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE)
End If
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
MakeTopMost()
End Sub
VS 2010 says "Function... doesn't return a value on all code paths. A null
reference exception could occur at run time when the result is used.
I have been at this for hours but annot figgure it out. Does anyone know?
Declaring array as static won't crash program
Declaring array as static won't crash program
When I initialise array of 1,000,000 integers, program crashes, but when I
put keyword static in front everything works perfectly, why?
int a[1000000] <- crash
static int a[1000000] <- runs correctly
When I initialise array of 1,000,000 integers, program crashes, but when I
put keyword static in front everything works perfectly, why?
int a[1000000] <- crash
static int a[1000000] <- runs correctly
Start another activity by clicking a button
Start another activity by clicking a button
So heres my problem. I set up a button that when clicked should open a new
activity but when it is clicked I receive an error: Unfortunately
"app_name" has stopped working my logcat says :Fatal Exception Main
So heres my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="58dp"
android:text="Monday"
android:textSize="20sp"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Tuesday" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Wednesday" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Thursday" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Friday" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Saturday" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Sunday" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="46dp"
android:text="Edit"
android:onClick="mondayintent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/button1"
android:text="Edit" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/button2"
android:text="Edit" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/button3"
android:text="Edit" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/button4"
android:text="Edit" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView6"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/button5"
android:text="Edit" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView7"
android:layout_alignBottom="@+id/textView7"
android:layout_alignLeft="@+id/button6"
android:text="Edit" />
</RelativeLayout>
and the second Xml to which im trying to reach in another activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textSize="40sp"
android:text="Monday" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:layout_toLeftOf="@+id/textView1"
android:ems="3"
android:inputType="time" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_centerHorizontal="true"
android:ems="3"
android:inputType="time" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/profileSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_toRightOf="@+id/textView1"
android:ems="3"
android:prompt="@string/profile"
android:entries="@array/profileSelector"
/>
</RelativeLayout>
Finally the code that supports both:
package com.example.hush;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Intent clickedDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
Button mondayEdit=(Button)findViewById(R.id.button1);
}
public void mondayintent()
{
clickedDay= new Intent(this,Monday.class);
startActivity(clickedDay);}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
second activity code:
package com.example.hush;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Monday extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.monday);
}
}
So heres my problem. I set up a button that when clicked should open a new
activity but when it is clicked I receive an error: Unfortunately
"app_name" has stopped working my logcat says :Fatal Exception Main
So heres my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="58dp"
android:text="Monday"
android:textSize="20sp"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Tuesday" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Wednesday" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Thursday" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Friday" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Saturday" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:text="Sunday" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="46dp"
android:text="Edit"
android:onClick="mondayintent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/button1"
android:text="Edit" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/button2"
android:text="Edit" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/button3"
android:text="Edit" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/button4"
android:text="Edit" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView6"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/button5"
android:text="Edit" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView7"
android:layout_alignBottom="@+id/textView7"
android:layout_alignLeft="@+id/button6"
android:text="Edit" />
</RelativeLayout>
and the second Xml to which im trying to reach in another activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textSize="40sp"
android:text="Monday" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="36dp"
android:layout_toLeftOf="@+id/textView1"
android:ems="3"
android:inputType="time" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_centerHorizontal="true"
android:ems="3"
android:inputType="time" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/profileSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_toRightOf="@+id/textView1"
android:ems="3"
android:prompt="@string/profile"
android:entries="@array/profileSelector"
/>
</RelativeLayout>
Finally the code that supports both:
package com.example.hush;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Intent clickedDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
Button mondayEdit=(Button)findViewById(R.id.button1);
}
public void mondayintent()
{
clickedDay= new Intent(this,Monday.class);
startActivity(clickedDay);}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
second activity code:
package com.example.hush;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Monday extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.monday);
}
}
Subscribe to:
Posts (Atom)