Bulls and Cows
==============
This is an ancient game with guessing numbers.
Draw 4-digit random number.
Print the following message:
A secret number of 4-digit has been draw!
Obtain a 4-digit number or "quit" from user by prompting following message repeatly:
Please enter a 4-digit number (or type 'quit' to exit):
If the input is "quit" show the following message (replace 0000 with the actual number being drawed
The secret number is: 0000
Show the number of digit(s) that have correct position as bull and the number of digit(s) that appeared without exact position as cow.
For instance: The random number is 1234 and the user input 1355. Exact position: "1", appeared without exact position: "3". The system
should display the following message:
1 bull 1 cow
If no digit found n the random number, the following should be shown instead:
Neither a bull nor a cow!
On the other hand, if the user guess the 4 digits correctly, the system should show the following message instead:
4 bulls! You are a genius!
Ask the user to play again if 4 digits have been guessed correctly by showing the following message:
Do you want to play a new game (Y/N)?
Show the following message if user has chosen to quit the program:
Thank you for playing Bulls and Cows!
Sample Input:
Sample Output:
Expected Test Command:
python BullsAndCowsPython32.py
import re,sys,random
print("Bulls and Cows")
print("==============")
print("This is an ancient game with guessing numbers.")
playNextGame="Y"
ans=""
while playNextGame.upper()=="Y" and ans!="quit":
secret=""
secretDigit=[]
for i in range(0,4):
secretDigit.append(random.randint(0,9))
secret+=str(secretDigit[i])
print("A secret number of 4-digit has been draw!")
bull=0
while bull!=4 and ans!="quit":
ans=""
while re.match("^\d{4}$|^quit$",ans)==None:
ans=input("Please enter a 4-digit number (or type 'quit' to exit):").strip()
if ans=="quit":
print("The secret number is: "+secret);
else:
cow=0
bull=0
arr=[]
for i in range (0,len(ans)):
arr.append(int(ans[i]))
if arr[i]==secretDigit[i]:
bull+=1
for i in range(0,4):
for j in range(0,4):
if arr[j]==secretDigit[i]:
cow+=1
arr[j]=-1
break
cow-=bull
msg=""
if cow==0 and bull==0:
msg="Neither a bull nor a cow!"
else:
if bull==4:
msg="4 bulls! You are a genius!"
else:
if cow==0:
if bull==1:
msg="1 bull only."
else:
msg=str(bull)+" bulls only."
else:
if bull==0:
if cow==1:
msg="1 cow only."
else:
msg="$cow cows only."
else:
if bull==1:
msg="1 bull and "
else:
msg=str(bull)+" bulls and "
if cow==1:
msg+=" 1 cow."
else:
msg+=str(cow)+" cows."
print(msg)
if ans!="quit":
playNextGame=""
while playNextGame.upper()!="Y" and playNextGame.upper()!="N":
playNextGame=input("Do you want to play a new game (Y/N)?").strip()
print("Thank you for playing Bulls and Cows!")
Date
2011/4/10
Name
BullsAndCows
Description
A number guessing game written in Python 2.5
Filename
BullsAndCowsPython25.py
Content
Print the following message:
Bulls and Cows
==============
This is an ancient game with guessing numbers.
Draw 4-digit random number.
Print the following message:
A secret number of 4-digit has been draw!
Obtain a 4-digit number or "quit" from user by prompting following message repeatly:
Please enter a 4-digit number (or type 'quit' to exit):
If the input is "quit" show the following message (replace 0000 with the actual number being drawed
The secret number is: 0000
Show the number of digit(s) that have correct position as bull and the number of digit(s) that appeared without exact position as cow.
For instance: The random number is 1234 and the user input 1355. Exact position: "1", appeared without exact position: "3". The system
should display the following message:
1 bull 1 cow
If no digit found n the random number, the following should be shown instead:
Neither a bull nor a cow!
On the other hand, if the user guess the 4 digits correctly, the system should show the following message instead:
4 bulls! You are a genius!
Ask the user to play again if 4 digits have been guessed correctly by showing the following message:
Do you want to play a new game (Y/N)?
Show the following message if user has chosen to quit the program:
Thank you for playing Bulls and Cows!
Sample Input:
Sample Output:
Expected Test Command:
python BullsAndCowsPython25.py
import re,sys,random
print("Bulls and Cows")
print("==============")
print("This is an ancient game with guessing numbers.")
playNextGame="Y"
ans=""
while playNextGame.upper()=="Y" and ans!="quit":
secret=""
secretDigit=[]
for i in range(0,4):
secretDigit.append(random.randint(0,9))
secret+=str(secretDigit[i])
print("A secret number of 4-digit has been draw!")
bull=0
while bull!=4 and ans!="quit":
ans=""
while re.match("^\d{4}$|^quit$",ans)==None:
ans=raw_input("Please enter a 4-digit number (or type 'quit' to exit):").strip()
if ans=="quit":
print("The secret number is: "+secret);
else:
cow=0
bull=0
arr=[]
for i in range (0,len(ans)):
arr.append(int(ans[i]))
if arr[i]==secretDigit[i]:
bull+=1
for i in range(0,4):
for j in range(0,4):
if arr[j]==secretDigit[i]:
cow+=1
arr[j]=-1
break
cow-=bull
msg=""
if cow==0 and bull==0:
msg="Neither a bull nor a cow!"
else:
if bull==4:
msg="4 bulls! You are a genius!"
else:
if cow==0:
if bull==1:
msg="1 bull only."
else:
msg=str(bull)+" bulls only."
else:
if bull==0:
if cow==1:
msg="1 cow only."
else:
msg="$cow cows only."
else:
if bull==1:
msg="1 bull and "
else:
msg=str(bull)+" bulls and "
if cow==1:
msg+=" 1 cow."
else:
msg+=str(cow)+" cows."
print(msg)
if ans!="quit":
playNextGame=""
while playNextGame.upper()!="Y" and playNextGame.upper()!="N":
playNextGame=raw_input("Do you want to play a new game (Y/N)?").strip()
print("Thank you for playing Bulls and Cows!")
Date
2011/4/9
Name
Add Two Numbers
Description
Add Two Numbers using Python 3.2
Filename
AddTwoNumPython32.py
Content
Using the Standard input stream to enter different numbers or text
Find two integers each time and,
Display output the sum to the standard output stream.
Repeat the all the steps until an EOF (End of File) signal received
Sample Input:
123 3.5 dsfasd 4 fdsafds 6
Sample Output:
126 0
Expected Test Command:
python AddTwoNumPython32.py < testdata.txt
import re,sys
nCount=0
total=0
try:
while True:
temp=input()
arr=re.split("\s+",temp)
for num in arr:
if num.isdigit():
if nCount % 2==0:
total=int(num)
else:
total+=int(num)
sys.stdout.write(str(total)+" ")
nCount+=1
except(EOFError):
pass
Date
2011/4/9
Name
Add Two Numbers
Description
Add Two Numbers using Python 2.5
Filename
AddTwoNumPython25.python
Content
Using the Standard input stream to enter different numbers or text
Find two integers each time and,
Display output the sum to the standard output stream.
Repeat the all the steps until an EOF (End of File) signal received
Sample Input:
123 3.5 dsfasd 4 fdsafds 6
Sample Output:
126 0
Expected Test Command:
python AddTwoNumPython25.py < testdata.txt
import re,sys
nCount=0
total=0
try:
while True:
temp=raw_input()
arr=re.split("\s+",temp)
for num in arr:
if num.isdigit():
if nCount % 2==0:
total=int(num)
else:
total+=int(num)
sys.stdout.write(str(total)+" ")
nCount+=1
except(EOFError):
pass
Date
2011/4/8
Name
Hello Friend
Description
Ask user's name and echo greeting message to user using Python 3.2
Filename
HelloFriendPython32.py
Content
Using the Standard output stream to prompt your:"Your name?"
Then use the input stream to receive input from user.
Display the greeting message "Hello, [user name]!" using standard output stream.
Repeat all the steps until users press CTRL-Z (MS windows) or CTRL-D (Mac) or receive the EOF signal.
Sample Input:
Peter Pan
Sample Output:
Hello, Peter Pan!
Expected Test Command:
python HelloFriendPython32.py < testdata.txt
try:
while True:
name=input("Your name?").strip()
print("Hello, "+name+"!")
except(EOFError):
pass
Date
2011/4/8
Name
Hello Friend
Description
Ask user's name and echo greeting message to user using Python 2.5
Filename
HelloFriendPython25.py
Content
Using the Standard output stream to prompt your:"Your name?"
Then use the input stream to receive input from user.
Display the greeting message "Hello, [user name]!" using standard output stream.
Repeat all the steps until users press CTRL-Z (MS windows) or CTRL-D (Mac) or receive the EOF signal.
Sample Input:
Peter Pan
Sample Output:
Hello, Peter Pan!
Expected Test Command:
python HelloFriendPython25.py < testdata.txt
try:
while True:
name=raw_input("Your name?")
print("Hello, "+name+"!")
except(EOFError):
pass
Date
2011/4/7
Name
Hello World
Description
Display greeting message to user using Python
Filename
HelloWorldPython.py
Content
Display the greeting message "Hello, World!" using standard output stream by Windows Scripting File using JScript