- Joined
- Dec 10, 2018
- Posts
- 86
- Reaction
- 33
- Points
- 103
Ayun mga ka phc, sorry sa late post. Daming activities sa school at daming kelangan i rewrite ng code.
So ayun nga, back to topic. Last time, napag aralan natin pano mag read ng text from a text file at pano mag write.
May mga kakaibang func kayong makikita dito tulad ng sum, for at kung ano ano pa. Pero di sya kelangan kung di kayo maarte katulad ko.
Ayaaan. Naka sulat na jan kung para san yung bawat function.
Ieexplain ko nalang ng medyo kung bat ako ba't ako nag open and close dun. Pag kasi binilang yung total # of lines, mababasa nya dapat yung buong text file at para magamit ko ulit yun o magsimula ulit sa line #1, sinarado ko sya at binuksan.
Gandang madaling araw sainyo! Cheers!
So ayun nga, back to topic. Last time, napag aralan natin pano mag read ng text from a text file at pano mag write.
May mga kakaibang func kayong makikita dito tulad ng sum, for at kung ano ano pa. Pero di sya kelangan kung di kayo maarte katulad ko.
Code:
import random
#import natin yung module na random for access ng random functions
import time
#for time modules.
#[4] ---
#Options if play or checkscores
opt = input('[1] Play now\n[2]Check Score\nChoice: ')
if opt == '1':
t = input('Number of tries: ')
rand_this = [1, 2, 3, 4, 5, 6]
#integers in a list
elif opt == '2':
chk = open('scores.txt')
lines = sum(1 for _ in chk)
chk.close()
#bibilangin yung total number of lines sa text file.
chk = open('scores.txt')
line_count = 0
print('Play history(latest is in the last.): ')
while line_count != lines:
line_count += 1
#line counter
sort = chk.readline()
#read current line
sort = sort.replace('\n', '')
#tatanggalin yung empty lines.
print(str(line_count) + '. ' + sort)
chk.close()
#isasara yung opened file
exit()
#ieexit yung program
else:
print('No such option.')
exit()
counter = 0
score_file = 'scores.txt'
score = 0
while counter != int(t):
#!= is not equal which is true/false based on user input.
guess = input('Guess a number from 1-6 \n')
#tatanungin kung anong hula mo.
#ang \n ay new line
result = random.choice(rand_this)
#pipili ang script ng choice dun sa list natin sa taas. RANDOMLY
if int(guess) == result:
print('You win!', guess, 'is', result)
score += 1
else:
print('Sorry.', guess, 'is not', result)
counter += 1
#instead of counter = counter + 1
#if and else statement. Yung nasa baba ay ang conclusions ang 'if' ay condition. Uri sya ng function.
#ang int() naman ay conversation from string. To integer.
#ang nasalabas ng while ay mag eexecute lamang kapag tapos na ang loop or false na.
f = open(score_file, 'a') #naka append
f.write(str(score) + '/' + t + '\n')
f.close()
print('Thankyou for playing.')
time.sleep(5)
#sleep for 5seconds.
Ieexplain ko nalang ng medyo kung bat ako ba't ako nag open and close dun. Pag kasi binilang yung total # of lines, mababasa nya dapat yung buong text file at para magamit ko ulit yun o magsimula ulit sa line #1, sinarado ko sya at binuksan.
Gandang madaling araw sainyo! Cheers!