I Haz Codes

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Menu
Reading from a file

Learn

Reading from a file requires you to understand how files work. In this lesson you will learn about file handles, open, close and readline.

Try

Write a program which will read in numbers from a file and add them up displaying a total.

When you read each line in you must cast it to an int().

Challenge

Create a file which contains 1 number from 1 to 100. Call this file “secret.txt”.

Write a program which will –

  • Give a player 5 lives, they lose a life for each incorrect guess.
  • Ask them to guess the secret number.
  • If the guess is too high, display “too high”.
  • If the guess is too low, display “too low”.
  • If the guess is correct, display “you have won”.
  • Keep going until they have won or they have run out of lives.

Comments are closed.

Information