Jump to content

finding first digit after decimal


picot

Recommended Posts

Hi,

You could convert the floating point into a string, then you could use the index() string function to first find the index position of the decimal point. Then with the index position (in your example it would be 2,) you could simply access the string by the index position - this is pseudo code:

myNumber = '1.29'

result = myNumber.index('.')
value = s[result +1]

 

Link to comment
Share on other sites

if not too much trouble please help me with one last question. I watched a youtube tutorial on how to make a dice game and I can not find anything on the internet to what I wanted to do. in my case I just used a single die:

import random

a = random.randint(1, 6) 
print(a) 


I know how to add each roll/result into a list but I wanted to ask how to add all the random rolls to the previous one? say 6 could be a game breaker (game ends if 6 is rolled and game resets to 0) otherwise, every roll adds to the last roll? thank you for your help advance

 

Edited by picot
Link to comment
Share on other sites

thank you for your quick response. my attempt was as you suggested both to exclude 6, and print add the sum of the rolled numbers as well... 
if a != 6:
	print(sum(a))
else:
	print ("you lose, your score for this last round = 0") 
but  print(sum(a)) doesn't seem right .. how do I add a number to a variable. many thanks!
Link to comment
Share on other sites

a = random.randint(1, 6) 

if a != 6:
	print(sum(a))
else:
	print ("you lose, your score for this last round = 0") 

in the above code, my attempt was to roll a single die, which will give a different number from 1 to 6 each time the program is run. I want to add those numbers each time its run (unless it's number 6, then the added numbers/scores reset)

say for example,  I run the program and I rolled the number 2, then I run the program again and roll 5, I was hoping the program to show the sum of the two separate rolls as '7' adding the separate runs... and  if I roll a third time and get '1'  it adds to the last score 7 to get an 8 and so on, (until a 6 is rolled in the random dice game ,,which breaks all the loop and the score will be 0)

Link to comment
Share on other sites

You need a variable to store the total of the non-six rolls and a loop so that it repeats until a 6 is rolled. 

I don't know python, so this is somewhat pseudocode - based on the code above...
 

total = 0
roll = 0

while ( roll != 6 ):
	roll = random.randint(1, 6)
	total = total + roll
    print("You rolled a ", roll, " Your total score is: ", total)

print("You rolled a 6, game over")

Edit:  My indentation is wrong ( I think Python cares about that stuff ) so copy / pasting my code probably won't work !

Edited by AndyYYZ
Indentation is wrong
  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...