Skip to content

Commit c6914f0

Browse files
committed
raw_input() was removed in Python 3
1 parent 6915a95 commit c6914f0

File tree

9 files changed

+75
-83
lines changed

9 files changed

+75
-83
lines changed

CountMillionCharacters-Variations/variation1.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import sys
2-
31
try:
4-
input_func = raw_input
5-
except:
6-
input_func = input
2+
input = raw_input
3+
except NameError:
4+
pass
75

86

97
def count_chars(filename):
@@ -21,7 +19,7 @@ def main():
2119
#Try to open file if exist else raise exception and try again
2220
while(is_exist):
2321
try:
24-
inputFile = input_func("File Name / (0)exit : ")
22+
inputFile = input("File Name / (0)exit : ").strip()
2523
if inputFile == "0":
2624
break
2725
print(count_chars(inputFile))

EncryptionTool.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
# 09/07/2017
55
from __future__ import print_function
66
import math
7-
import sys
87

9-
input_fun = None
10-
key = int(math.pi * 1e14)
11-
12-
if sys.version_info.major >= 3:
13-
input_fun = input
8+
try:
9+
input = raw_input
10+
except NameError:
11+
pass
1412

15-
else:
16-
input_fun = raw_input
17-
18-
text = input_fun("Enter text: ")
13+
key = int(math.pi * 1e14)
14+
text = input("Enter text: ")
1915
values = []
2016
reverse = []
2117

GroupSms_Way2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
from getpass import getpass
55
import sys
66

7+
try:
8+
input = raw_input
9+
except NameError:
10+
pass
11+
712
username = input('Enter mobile number:')
813
passwd = getpass()
914
message = input('Enter Message:')
1015
#Fill the list with Recipients
11-
x=raw_input('Enter Mobile numbers seperated with comma:')
16+
x=input('Enter Mobile numbers seperated with comma:')
1217
num=x.split(',')
1318
message = "+".join(message.split(' '))
1419

TicTacToe.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Tic Tac Toe
22

33
import random
4-
import sys
54

6-
def get_input():
7-
if sys.version_info >= (3, 0):
8-
return input()
9-
else:
10-
return raw_input()
5+
try:
6+
input = raw_input
7+
except NameError:
8+
pass
119

1210
def drawBoard(board):
1311
# This function prints out the board that it was passed.
@@ -23,27 +21,21 @@ def inputPlayerLetter():
2321
# Lets the player type which letter they want to be.
2422
# Returns a list with the player's letter as the first item, and the computer's letter as the second.
2523
letter = ''
26-
while not (letter == 'X' or letter == 'O'):
27-
print('Do you want to be X or O?')
28-
letter = get_input().upper()
24+
while letter not in ('X', 'O'):
25+
letter = input('Do you want to be X or O? ').upper()
2926

3027
# the first element in the tuple is the player's letter, the second is the computer's letter.
31-
if letter == 'X':
32-
return ['X', 'O']
33-
else:
34-
return ['O', 'X']
28+
29+
return ['X', 'O'] if letter == 'X' else ['O', 'X']
3530

3631
def whoGoesFirst():
3732
# Randomly choose the player who goes first.
38-
if random.randint(0, 1) == 0:
39-
return 'computer'
40-
else:
41-
return 'player'
33+
return random.choice(('computer', 'player'))
4234

4335
def playAgain():
4436
# This function returns True if the player wants to play again, otherwise it returns False.
4537
print('Do you want to play again? (yes or no)')
46-
return get_input().lower().startswith('y')
38+
return input().lower().startswith('y')
4739

4840
def makeMove(board, letter, move):
4941
if isSpaceFree(board,move):
@@ -81,28 +73,18 @@ def getPlayerMove(board):
8173
move = ' '
8274
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
8375
print('What is your next move? (1-9)')
84-
move = get_input()
76+
move = input()
8577
return int(move)
8678

8779
def chooseRandomMoveFromList(board, movesList):
8880
# Returns a valid move from the passed list on the passed board.
8981
# Returns None if there is no valid move.
90-
possibleMoves = []
91-
for i in movesList:
92-
if isSpaceFree(board, i):
93-
possibleMoves.append(i)
94-
95-
if len(possibleMoves) > 0:
96-
return random.choice(possibleMoves)
97-
else:
98-
return None
82+
possibleMoves = [i for i in movesList if isSpaceFree(board, i)]
83+
return random.choice(possibleMoves) if possibleMoves else None
9984

10085
def getComputerMove(board, computerLetter):
10186
# Given a board and the computer's letter, determine where to move and return that move.
102-
if computerLetter == 'X':
103-
playerLetter = 'O'
104-
else:
105-
playerLetter = 'X'
87+
playerLetter = 'O' if computerLetter == 'X' else 'X'
10688

10789
# Here is our algorithm for our Tic Tac Toe AI:
10890
# First, check if we can win in the next move

dice_rolling_simulator.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
import random
99

10+
try:
11+
input = raw_input
12+
except NameError:
13+
pass
14+
1015
global user_exit_checker
1116
user_exit_checker = "exit"
1217

@@ -15,27 +20,24 @@
1520

1621

1722
def start():
18-
print
19-
"Welcome to dice rolling simulator: \nPress Enter to proceed"
20-
raw_input(">")
23+
print("Welcome to dice rolling simulator: \nPress Enter to proceed")
24+
input(">")
2125

2226
# Starting our result function (The dice picker function)
2327
result()
2428

2529

2630
# Our exit function (What the user will see when choosing to exit the program)
2731
def bye():
28-
print
29-
"Thanks for using the Dice Rolling Simulator! Have a great day! =)"
32+
print("Thanks for using the Dice Rolling Simulator! Have a great day! =)")
3033

3134

3235
# Result function which is our dice chooser function
3336
def result():
3437
# user_dice_chooser No idea how this got in here, thanks EroMonsterSanji.
3538

36-
print
37-
"\r\nGreat! Begin by choosing a die! [6] [8] [12]?\r\n"
38-
user_dice_chooser = raw_input(">")
39+
print("\r\nGreat! Begin by choosing a die! [6] [8] [12]?\r\n")
40+
user_dice_chooser = input(">")
3941

4042
user_dice_chooser = int(user_dice_chooser)
4143

@@ -51,40 +53,36 @@ def result():
5153

5254
# If the user doesn't choose an applicable option
5355
else:
54-
print
55-
"\r\nPlease choose one of the applicable options!\r\n"
56+
print("\r\nPlease choose one of the applicable options!\r\n")
5657
result()
5758

5859

5960
# Below are our dice functions.
6061
def dice6():
6162
# Getting a random number between 1 and 6 and printing it.
6263
dice_6 = random.randint(1, 6)
63-
print
64-
"\r\nYou rolled a " + str(dice_6) + "!\r\n"
64+
print("\r\nYou rolled a " + str(dice_6) + "!\r\n")
6565

6666
user_exit_checker()
6767

6868

6969
def dice8():
7070
dice_8 = random.randint(1, 8)
71-
print
72-
"\r\nYou rolled a " + str(dice_8) + "!"
71+
print("\r\nYou rolled a " + str(dice_8) + "!")
7372

7473
user_exit_checker()
7574

7675

7776
def dice12():
7877
dice_12 = random.randint(1, 12)
79-
print
80-
"\r\nYou rolled a " + str(dice_12) + "!"
78+
print("\r\nYou rolled a " + str(dice_12) + "!")
8179

8280
user_exit_checker()
8381

8482

8583
def user_exit_checker():
8684
# Checking if the user would like to roll another die, or to exit the program
87-
user_exit_checker_raw = raw_input("\r\nIf you want to roll another die, type [roll]. To exit, type [exit].\r\n?>")
85+
user_exit_checker_raw = input("\r\nIf you want to roll another die, type [roll]. To exit, type [exit].\r\n?>")
8886
user_exit_checker = (user_exit_checker_raw.lower())
8987
if user_exit_checker == "roll":
9088
start()

dir_test.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
# Description : Tests to see if the directory testdir exists, if not it will create the directory for you
99
from __future__ import print_function
10-
import os # Import the OS Module
11-
import sys
10+
import os
1211

12+
try:
13+
input = raw_input
14+
except NameError:
15+
pass
1316

14-
def main():
15-
if sys.version_info.major >= 3: # if the interpreter version is 3.X, use 'input',
16-
input_func = input # otherwise use 'raw_input'
17-
else:
18-
input_func = raw_input
1917

20-
CheckDir = input_func("Enter the name of the directory to check : ")
18+
def main():
19+
CheckDir = input("Enter the name of the directory to check : ")
2120
print()
2221

2322
if os.path.exists(CheckDir): # Checks if the dir exists

pscheck.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
import os
1313
import string
1414

15+
try:
16+
input = raw_input
17+
except NameError:
18+
pass
19+
1520

1621
def ps():
17-
program = raw_input("Enter the name of the program to check: ")
22+
program = input("Enter the name of the program to check: ")
1823

1924
try:
2025
# perform a ps command and assign results to a list

spotlight.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import hashlib
77
from PIL import Image
88

9+
try:
10+
input = raw_input
11+
except NameError:
12+
pass
13+
914
def md5(fname):
1015
""" Function to return the MD5 Digest of a file """
1116

@@ -60,9 +65,6 @@ def get_spotlight_wallpapers(target_folder):
6065
shutil.copy(filename, temp_path+".png")
6166

6267
if __name__ == '__main__':
63-
PATH = raw_input("Enter directory path:")
68+
PATH = input("Enter directory path:").strip()
6469
get_spotlight_wallpapers(PATH)
6570
print("Lockscreen images have been copied to \""+PATH+"\"")
66-
67-
68-

tweeter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
Tweet text and pics directly from the terminal.
88
"""
99
from __future__ import print_function
10-
import tweepy, os
10+
import os
11+
import tweepy
12+
13+
try:
14+
input = raw_input
15+
except NameError:
16+
pass
17+
1118

1219
def getStatus():
1320
lines = []
1421
while True:
15-
line = raw_input()
22+
line = input()
1623
if line:
1724
lines.append(line)
1825
else:
@@ -31,7 +38,7 @@ def tweetthis(type):
3138
return
3239
elif type == "pic":
3340
print("Enter pic path "+user.name)
34-
pic = os.path.abspath(raw_input())
41+
pic = os.path.abspath(input())
3542
print("Enter status "+user.name)
3643
title = getStatus()
3744
try:
@@ -56,7 +63,7 @@ def initialize():
5663
user = api.me()
5764

5865
def main():
59-
doit = int(raw_input("\n1. text\n2. picture\n"))
66+
doit = int(input("\n1. text\n2. picture\n"))
6067
initialize()
6168
if doit == 1:
6269
tweetthis("text")

0 commit comments

Comments
 (0)