dimanche 1 mars 2015

python while and if with pairs of conditions

I can't figure out why this is not working, I've looked at how the "if any([])" syntax is used but in my case I have conditions that come in pairs.


I am trying to generate a random sequence of arrows where all combinations are allowed except the negation of the last arrow (so if the first arrow is L, then the next arrow cannot be R). The code should be keeping Ch2 = 0 if a disallowed sequence occurs and hence in the while loop, otherwise it should set Ch2 = 1 and then I can write code to move on to the next arrow in the sequence.


Also I'm sure there is a better way to do this, but I am just learning Python.



Arrow_Array = ['L.png', 'R.png', 'U.png', 'D.png']
Ch2 = 0

Choice1 = random.choice(Arrow_Array)

while Ch2 != 1:
Choice2 = random.choice(Arrow_Array)
if any([Choice1 == 'L.png' and Choice2 == 'R.png', Choice1 == 'R.png' and Choice2 == 'L.png', Choice1 == 'U.png' and Choice2 == 'D.png', Choice1 == 'D.png' and Choice2 == 'U.png']):

Ch2 = 0
else:
Ch2 = 1

Button in if loop

I'm working on a trivia code in javascript/html for a class i am taking, and when you press the correct button, i want an image of a check mark to show up. Right now, for my check mark i am using the "opacity" trait:



<img id="checkOne" src="check.jpg" style="opacity: 0">


and then later i wanted to add that if you click the right button, the opacity would change from O to 1 (if you know a better way than this that's fine, I'm totally okay with changing that).


My question: How can i say "if this button is clicked, then change the opacity to 1/put this image on the webpage. else console log try again." Help?


C programming help - Adding Values together

So I have this code but I'm missing something and I can't quite seem to get it. The point of this is to prompt the user for a selection. They pick the food that they want to order, and it tells them it is added. When the user hits 0 the program ends by saying what the total is of all the items they chose. For some reason when I hit the end, it just states the total of the last item added. Any ideas on how to fix this so that the totals all add up?



#include<stdio.h>

#define SALES_TAX .06

int selection;
double total;
double amount;
int i;

double getPrice(int selection);


void printOptionName(int selection);


void printMenu();


double getPrice(int selection){

if (selection == 1){
amount = 5.99;
} else if (selection == 2){
amount = 6.99;
} else if (selection == 3){
amount = 7.99;
} else if (selection == 4){
amount = 10.50;
} else if (selection == 5){
amount = 3.50;
}

}

void printOptionName(int selection){

if (selection == 0){
printf("\nYour total is: ");
} else if (selection == 1){
printf("\nAdded a Small Pizza\n\n");
} else if (selection == 2){
printf("\nAdded a Medium Pizza\n\n");
} else if (selection == 3){
printf("\nAdded a Large Pizza\n\n");
} else if (selection == 4){
printf("\nAdded an order of Wings\n\n");
} else if (selection == 5){
printf("\nAdded a Drink\n\n");
} else
printf("Not a valid selection. Please select one of the following options: \n");
}


void printMenu(){

printf("0. (Complete Order)\n");
printf("1. Small Pizza ****** 5.99\n");
printf("2. Medium Pizza ****** 6.99\n");
printf("3. Large Pizza ****** 7.99\n");
printf("4. Wings ****** 10.50\n");
printf("5. Drink ****** 3.50\n");
printf("Enter the Number of your selection: \n");

}

int main(){

printMenu();
scanf("%d", &selection);
printOptionName(selection);
getPrice(selection);

while (selection != 0){
printMenu();
scanf("%d", &selection);
printOptionName(selection);
getPrice(selection);
}

for (i = 0; i <= selection; i++){
total = total + amount;
}

printf("%lf\n", total);

return 0;
}

Creating a menu with arrays - sorting results alphabetically and storing, loading from a file

I'm trying to create a menu system for a hotel program using arrays; below are my hotel booking options:



A: To Add a customer to a room
V: To View all the rooms
E: To Display all the empty rooms
D: To Delete a customer from a room
F: Find a room from a customers name
S: Store program array data into a plain text file
L: Load program data back from the file into the array
O: View rooms alphabetically by name
e: To end


I've seem to be able to do from A-F but i'm having trouble ordering the rooms alphabetically and sorting and loading the program array data from a file. Completely lost on the S and L front. A friend has helped with S,L and O but i'm not entirely sure why its not working so i have had to comment it out. If you can help fix the already given solution or something new close to the style i have written the rest of my program in that would be great.



public class Hotel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//allows you to input selection later

String roomName;
int roomNum = 0;

String[] hotel = new String[10];
//for (int x = 0; x < 10; x++ ) hotel[x] = ""

initialise(hotel); //better to initialise in a procedure

String MenuChoice; //calling menu choice a String
char MC; //Reffering menu choice (String) as char

do {
System.out.println("Hotel Booking Options");
System.out.println("=====================================");
System.out.println("A: To Add a customer to a room");
System.out.println("V: To View all the rooms");
System.out.println("E: To Display all the empty rooms");
System.out.println("D: To Delete a customer from a room");
System.out.println("F: Find a room from a customers name");
System.out.println("S: Store program array data into a plain text file");
System.out.println("L: Load program data back from the file into the array");
System.out.println("O: View rooms alphabetically by name");
System.out.println("e: To end ");
System.out.println(" ");
System.out.println("Please choose the required option and enter below: ");
//Print statement
MenuChoice = input.next();
MC = MenuChoice.charAt(0); //(MC)MenuChoice = MenuChoice.charAt(0);

switch (MC) {
case 'A':
addCustomerToRoom(hotel);//calls the addCustomerToRoom statement below
break; //case break A
case 'V':
viewAllRooms(hotel); //calls the viewAllRooms statement below
break; //case break V
case 'E':
displayEmptyRooms(hotel); //calls the displayEmptyRooms statement below
break; //case break E
case 'D':
deleteCustomer(hotel); //calls the deleteCustomer statement below
break; //case break D
case 'F':
findCustomer(hotel); //calls the findCustomer statement below
break; //case break F
case 'S':
// storeInTextFile(hotel); //calls the deleteCustomer statement below
break; //case break S
case 'L':
// loadFromTextFile(hotel); //calls the findCustomer statement below
break; //case break L
case 'O':
// orderAlphabetically(hotel);
break; //case break O
} //end menuChoice MC
System.out.print(" ");
} while (MC != 'e');
}

private static void initialise(String hRef[]) {
for (int x = 0; x < 10; x++) {
hRef[x] = "no customers";
} //end for loop
System.out.println("initilise ");
} //end initialise

private static void viewAllRooms(String hRef[]) {
for (int x = 0; x < 10; x++) {
if (hRef[x].equals("e")) {
System.out.println("Room " + x + " is empty ");
} //end if statement
else {
System.out.println("Room " + x + " is currently occupied by " + hRef[x]);
} //end else statement
} //end for loop
} //end viewAllRooms

private static void addCustomerToRoom(String hRef[]) {
Scanner input = new Scanner(System.in);
int rNumRef; //different variable to roomNum in Main method
String rNameRef;

System.out.println("Enter room number (0-9) or 10 to stop:");
rNumRef = input.nextInt(); //input the room number

System.out.println("Enter name for room " + rNumRef + " :");
rNameRef = input.next(); //input the customer name
hRef[rNumRef] = rNameRef; //both variables are the same

} //end addCustomerToRoom

private static void displayEmptyRooms(String hRef[]) {
for (int x = 0; x < 10; x++) {
if (hRef[x].equals("no customers")) {
System.out.println("Room " + x + " is empty ");
} //end if statement
} //end for loop
} //end displayEmptyRooms

private static void deleteCustomer(String hRef[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Customer Name");
String rNameRef;
rNameRef = input.next(); //input the customer name

for (int x = 0; x < 10; x++) {
if (hRef[x].equals(rNameRef)) {
hRef[x] = "no customers";
System.out.println("Customer " + rNameRef + " has been removed from Room " + x);
} //end if statement
} //end for loop
} //enddeleteCustomer

private static void findCustomer(String hRef[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Customer Name");
String rNameRef;
rNameRef = input.next(); //input the customer name

for (int x = 0; x < 10; x++) {
if (hRef[x].equalsIgnoreCase(rNameRef)) {
System.out.println("Room " + x + " is currently occupied by " + hRef[x]);
} //end if statement
} //end for loop
} //end findCustomer

// private static void storeInTextFile(String hRef[]) throws FileNotFoundException {
// String HotelFile = "sec/txtFile/HotelCoursework.txt";
// try (PrintStream HotelStream = new PrintStream(HoteFile))
// for (int x = 0; x < 10; x++) {
// HotelStream.println(/*x + " " +*/hRef[x]);
// }
// HotelStream.close();
// }
// /*String HotelFile = "H:HotelCoursework.txt";
// PrintStream HotelStream = new PrintStream (HotelFile);
// String rNameRef = new String();
// for (int x = 0; x < 10; x++) (HotelStream.println(/*x + " " + hRef[x]);
// HotelStream.close();
// */
// } //end storeInTextFile
// private static void loadFromTextFile(String hRef[]) throws FileNotFoundException {
// String HotelScan = new Scanner(new BufferedReader(new FileReader("sec/txtFile/HotelCoursework.txt")));
// for (int jj = 0; j < 10; j++) {
// hRef[jj] = HotelScan.nextLine();
// } //end for loop
// }//end loadFromTextFile
//
// private static void orderAlphabetically(String hRef[]) {
// boolean flag = true;
// String temp;
// int j;
// while (flag) {
// flag = false;
// for (j = 0; j < hotel.length - 1; j++) {
// if (hRef[j].compareToIgnoreCase(hRef[j + 1]) > 0) {
// temp = hRef[j];
// hRef[j] = hRef[j + 1];
// hRef[j + 1] = temp;
// flag = true;
// } //end if statement
// } //end for loop
// } //end while loop
// } //end orderAlphabetically
} //end public class

batch file string comparison if condition for loop

If condition of the code below does not work. why? The aim of the code is to list names of directories included in a directory, name of which not start with underscore.


code:



@ECHO off
setlocal enabledelayedexpansion
SET "ptka="
SET "ptkb="
FOR /d %%a IN (*) DO (
SET "ptka=%%a"
SET "ptkb=!ptka:~0,1!"
IF NOT "!ptkb!"=="_" ECHO !ptka!>>list.txt :: ("!ptkb!"=="_") part is not working
)

Solution for if else and NSString statement

What NSString do I put for return? The answer I am looking for is 1 cheese OR 2 cheeses



- (NSString *) numberOfCheesesStringWithCheeseCount:(NSUInteger)cheeseCount {
if (cheeseCount == 1) {
/* WORK HERE, ASSUMING THERE IS 1 CHEESE */
NSString *phrase = [NSString stringWithFormat:@"%ld", (long)cheeseCount];
NSLog(@"%@ cheese", phrase);
} else {
/* WORK HERE, ASSUMING THERE ARE 2+ CHEESES */
NSString *phrase2 = [NSString stringWithFormat:@"%ld", (long)cheeseCount];
NSLog(@"%@ cheeses", phrase2);
}

/*

(You will learn more about if/else statements in the next checkpoint.)
*/

return ;
}

How to add in if else statement to validate for string

I am using BlueJ for this assignment I have and I have a problem doing this part of the question which is to put decision constructs to ensure that invalid data is not set which I have already tried.In which I placed the if else statement in the setName portion which does not work. The code will show error if I have put 1 in the GraphicIllustrators main void portion for setName. So where do I need to put the if else statement in the code below.BTW I am using inheritance to do this.So please advise.Thanks!


The coding for the main class:



public class Publishing_Inc
{
private int ID=0;
private String name="-Name Needed-";
private int level=0;
private String jobtitle="-Title Needed-";
private String edit="-Edit Skill-";

public void calculateID(){
int uniqueID;
uniqueID =((int)( Math.random()*10000)+1);
ID = uniqueID;
}
public int getID(){
return ID;
}



public void setName(String d) {
name = d;
}
public String getName() {
return name;
}
public void setTitle(String b){
jobtitle=b;
}
public String getTitle() {
return jobtitle;
}

public void calculatelevel(){
int uniquelevel;
uniquelevel =((int)( Math.random()*3)+1);
level = uniquelevel;
}
public int getlevel() {
return level;
}

public void setEdit(String z){
edit=z;
}
public String getEdit() {
return edit;
}


}


The sub class:



public class GraphicIllustrators extends Publishing_Inc
{
public void displayGraphInformation() {
System.out.println("ID: " + getID());
System.out.println("Name:" + getName());
System.out.println("Job Title: " + getTitle());
System.out.println("Level: " + getlevel());
System.out.println();
}

public static void main (String args[]) {
GraphicIllustrators graphic = new GraphicIllustrators ( );
graphic.calculateID ( );
graphic.setName (" Tim Cook" );
graphic.calculatelevel ();
graphic.setTitle ("Graphic Illustrators" );
graphic.displayGraphInformation( );
}

}