picot Posted October 30, 2017 Report Share Posted October 30, 2017 I wanted to ask how you can find the first digit after decimal point on python from a user input. for example if user inputs 1.299999 I want to find ''2'' please help. thank you. Link to comment Share on other sites More sharing options...
administrator Posted October 30, 2017 Report Share Posted October 30, 2017 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 More sharing options...
picot Posted November 6, 2017 Author Report Share Posted November 6, 2017 I appreciate it! thank you! Link to comment Share on other sites More sharing options...
picot Posted November 6, 2017 Author Report Share Posted November 6, 2017 (edited) 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 November 6, 2017 by picot Link to comment Share on other sites More sharing options...
administrator Posted November 6, 2017 Report Share Posted November 6, 2017 Just use an 'if' statement and check to see if 6 is rolled ... if that makes sense? if number_rolled == 6: # do this else: # do that instead Stef Link to comment Share on other sites More sharing options...
picot Posted November 6, 2017 Author Report Share Posted November 6, 2017 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 More sharing options...
administrator Posted November 6, 2017 Report Share Posted November 6, 2017 Create the variable outside of the conditional so you can update it within the conditional. Something like: a_number = 6 if a_number != 6: print(sum(a)) else: print ("you lose, your score for this last round = 0") Not sure if I understand your question totally. Stef Link to comment Share on other sites More sharing options...
picot Posted November 6, 2017 Author Report Share Posted November 6, 2017 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 More sharing options...
AndyYYZ Posted November 7, 2017 Report Share Posted November 7, 2017 (edited) 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 November 8, 2017 by AndyYYZ Indentation is wrong 1 Link to comment Share on other sites More sharing options...
Recommended Posts