0

I've recently started learning python from a book (invent your own computer games with python by Al Sweigart) and I'm trying to use stuff that I learn to modify exercises that I've done to make them more to my liking. Anyway, there was one such script which I tried to modify and while the modified version runs just the way I like it when I'm trying to run it using the interactive shell, when I double click the script icon to have it run on the command line interface(I hope I'm using the correct terminology so far cause I'm not really familiar with programming as of yet) it doesn't run. The command window opens and nothing happens.

Here's the original script that runs on both the shell and the command line:

import random
import time
def displayIntro():
 print('''You are in a land full of dragons. In front of you, you see two caves. in one cave, the dragon is friendly and will share his treasure with you. the other dragon is greedy and hungry and will eat you on sight.''')
 print()
def chooseCave():
 cave=''
 while cave != '1' and cave!='2':
 print('Which cave will you go into?(1 or 2)')
 cave=input()
 return cave
def checkCave(chosenCave):
 print('you approach the cave...')
 time.sleep(2)
 print('it\'s dark and spooky')
 time.sleep(2)
 print('a large dragon jumps out in front of you! he opens his jaws and...')
 print()
 time.sleep(2)
 friendlyCave=random.randint(1,2)
 if chosenCave==str(friendlyCave):
 print('gives you his treasure!')
 else:
 print('gobbles you down in 1 bite!')
playAgain = 'yes'
while playAgain=='yes' or playAgain=='y':
 displayIntro()
 caveNumber=chooseCave()
 checkCave(caveNumber)
 print('Do you want to play again?(yes or no)')
 playAgain=input()

And this is the modified version (I wanted the text to appear as being typed on the go to make it look more immersive:

import random
import time
def read(string):
 i=0
 while i<len(string):
 print(string[i],end='')
 time.sleep(0.05)
 if string[i]=='.':
 time.sleep(0.5)
 i=i+1
 print('')
def displayIntro():
 intro='''You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight.'''
 i=0
 read(intro)
def chooseCave():
 cave=''
 i=0
 question='Which cave will you go into? (1 or 2)'
 j=0
 print('')
 read(question)
 print('')
 while cave != '1' and cave != '2' and i<=10:
 cave = input()
 i=i+1
 return cave
def checkCave(chosenCave):
 approach='You approach the cave...'
 j=0
 read(approach)
 print()
 spooky='It\'s dark and spooky...'
 j=0
 read(spooky)
 time.sleep(1)
 print()
 print('\nA large dragon jumps out in front of you! He opens his jaw and...')
 time.sleep(1.5)
 friendlyCave=random.randint(1,2)
 if chosenCave == str(friendlyCave):
 print('Gives you his treasure!')
 else:
 print('Gobbles you down in one bite!')
playAgain='yes'
while playAgain=='yes' or playAgain== 'y':
 displayIntro()
 caveNumber=chooseCave()
 checkCave(caveNumber)
 print('Do you want to play again? (yes or no)')
 playAgain = input()

I tried removing the "end=''" part from the print(string[i],end='') line and it did run normally!(Although with terrible results as it typed 1 character per line!)

What do you think is the issue and how can I fix it without making it type a single character per line?

Thanks for your time! Bill

(Ps: during formatting the code for the post, I had to only indent lines that were not already intdented, so I think there might be an issue with indentation when trying to copy the code as some lines lack indentation? Anyway I hope this is not an issue)

Oluwafemi Sule
39.3k1 gold badge63 silver badges88 bronze badges
asked Sep 22, 2017 at 3:12
4
  • can you write code with proper indentation? Commented Sep 22, 2017 at 3:19
  • I'm sorry, I've added the correct indentation(I think). Commented Sep 22, 2017 at 3:28
  • No you did not. you need to add 4 spaces for every line of your code for proper indentation. Commented Sep 22, 2017 at 3:31
  • Forgot the last 4 lines, just double checked and fixed them! Commented Sep 22, 2017 at 3:39

3 Answers 3

1

You need to import sys and use sys.stdout.flush() function to get the flow of characters you want.

The read function should look like this

import random
import time
import sys
def read(string):
 i = 0
 while i < len(string):
 print(string[i], end='')
 time.sleep(0.05)
 if string[i] == '.':
 time.sleep(0.5)
 # flush stdout after sleep
 sys.stdout.flush()
 i = i + 1
 print('')
[... rest of the code ...]

It is good practice (PEP8) to have spaces between math symbols and conditional operators like the following

def chooseCave():
 [... code ...]
 i = 0
 [... code ...]
 while cave != '1' and cave != '2' and i <= 10:
 [... code ...]

Another PEP8 good practice is to not pass 79 maximum line length. So, when you have a really long string, one way to not pass the 79 characters is to do the following

def displayIntro():
 intro = ('You are in a land full of dragons. In front of you, you see two '
 'caves. In one cave, the dragon is friendly and will share his '
 'treasure with you. The other dragon is greedy and hungry, and '
 'will eat you on sight.')
 read(intro)
Sign up to request clarification or add additional context in comments.

Comments

0

The print() statement at the end of "read" function will print a new line.

The whole code without the print():

import random
import time
def read(string):
 i=0
 while i<len(string):
 print(string[i],end='')
 time.sleep(0.05)
 if string[i]=='.':
 time.sleep(0.5)
 i=i+1
def displayIntro():
 intro='''You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight.'''
 i=0
 read(intro)
def chooseCave():
 cave=''
 i=0
 question='Which cave will you go into? (1 or 2)'
 j=0
 print('')
 read(question)
 print('')
 while cave != '1' and cave != '2' and i<=10:
 cave = input()
 i=i+1
 return cave
def checkCave(chosenCave):
 approach='You approach the cave...'
 j=0
 read(approach)
 print()
 spooky='It\'s dark and spooky...'
 j=0
 read(spooky)
 time.sleep(1)
 print()
 print('\nA large dragon jumps out in front of you! He opens his jaw and...')
 time.sleep(1.5)
 friendlyCave=random.randint(1,2)
 if chosenCave == str(friendlyCave):
 print('Gives you his treasure!')
 else:
 print('Gobbles you down in one bite!')
playAgain='yes'
while playAgain=='yes' or playAgain== 'y':
 displayIntro()
 caveNumber=chooseCave()
 checkCave(caveNumber)
 print('Do you want to play again? (yes or no)')
 playAgain = input()
answered Sep 22, 2017 at 3:30

2 Comments

But what I want to do with the read function is to print 1 character at a time with a small time interval between them to make it look like someone's typing the text instead of having it appear instantly. It's working normally just as I like it when trying to run using the python shell, but not when I double click on the script icon.
Okay got it. Edited the answer. This will print the text like as you are typing. But not sure about your double clicks :)
0

Well... I tried to solve your problem. I wrote your code in my way but it works in the python shell but not in the command prompt window. However, here is my code:

import random as rnd
import time
import msvcrt
def read(string):
 for each in string:
 print(each, end="")
 time.sleep(0.05)
def displayIntro():
 intro="You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight."
 read(intro)
def chooseCave():
 cave=''
 print()
 while cave != '1' and cave!='2':
 read('Which cave will you go into?(1 or 2): ')
 cave=input()
 return cave
def checkCave(chosenCave):
 read('you approach the cave...')
 print()
 time.sleep(2)
 read("it's dark and spooky")
 print()
 time.sleep(2)
 read('a large dragon jumps out in front of you! he opens his jaws and...')
 print()
 print()
 time.sleep(2)
 friendlyCave=rnd.randint(1,2)
 if chosenCave==str(friendlyCave):
 read('gives you his treasure!')
 else:
 read('gobbles you down in 1 bite!')
 print()
playAgain="yes"
while playAgain=="yes" or playAgain=="y":
 displayIntro()
 caveNumber=chooseCave()
 checkCave(caveNumber)
 read("Do you want to play again? (yes/y or no/n): ")
 playAgain=input()
 playAgain=playAgain.lower()
print("Press any key to continue...")
while not msvcrt.kbhit():
 time.sleep(0.1)

I think you should learn GUI(Graphical User Interface) using tkinter (because using tkinter is simple and easy (at least for me)). If you learn GUI, you'll be able to make your programs more interesting.

answered Sep 22, 2017 at 4:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.