import random def write(n): file=open(\'randnum.txt\', \'w\') for i in range(n):
ID: 3853585 • Letter: I
Question
import random
def write(n):
file=open('randnum.txt', 'w')
for i in range(n):
file.write(str(random.randint(0,999)) +' ')
file.close()
def read():
file=open('randnum.txt', 'r')
i=0
for line in file:
print ('line')
i+=1
print ('total number of random number is ')+str(i)
def main():
while(True):
print(' Enter total random numbers you want to write to file')
n=raw_input()
status= True
if(n>0):
for ch in n:
if(ord(ch)>=48 and ord(ch)<=57):
continue
else:
status=False
print('Enter a valid positive Interger')
break
else:
print('Number must be positive and greater than zero')
read()
main()
what is wrong with my program - it says raw input is not defined?
Explanation / Answer
In Python 3.x, raw_input() has been renamed to input().
We can cast according to our needs.
For example, we are taking total random numbers from the console(keyboard). So it must be integer
So we can cast it to:
int(raw_input()) for integers. or we can direclty use n=input();
In Python 3.x, input() replaces raw_input(), for input from the console. It returns the user's response a string, so when an int or a float is needed, it is necessary to convert the returned value from the str type using int() or float().
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.